Recent

Author Topic: is operator for classes  (Read 388 times)

doconnor

  • New member
  • *
  • Posts: 7
is operator for classes
« on: April 11, 2021, 07:32:21 pm »
Currently the is operator tells you is a object is a class or one its descendants. Would it be possible do the same with another class, as in

<class> is <class>

I use class types and have run into this limitation from time to time over the years.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 870
Re: is operator for classes
« Reply #1 on: April 11, 2021, 07:48:26 pm »
Try <class> is <class>.classtype

doconnor

  • New member
  • *
  • Posts: 7
Re: is operator for classes
« Reply #2 on: April 11, 2021, 09:37:17 pm »
I have discovered the InheritsFrom class function which pretty much the same as is, but works on classes as well.

lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: is operator for classes
« Reply #3 on: April 11, 2021, 09:58:00 pm »
Try <class> is <class>.classtype

If that really works maybe an example should be added to section 12.8.7 Class operators of the Language Guide.

While it says there:
Quote from: Free Pascal Reference guide
The is operator can only be used with a class reference or a class instance
[emphasis mine]
it's not very clear how that's done, and all the current examples are with object instances.

I have discovered the InheritsFrom class function which pretty much the same as is, but works on classes as well.

Of course; they are equivalent. From the same page of the Language Guide:
Quote
The usage of this operator is as follows:
  Object is Class

This expression is completely equivalent to
  Object.InheritsFrom(Class)

If Object is Nil, False will be returned.

Which paragraph, incidentally, again begs the question of how "is" can be used with a class reference :-\
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018