[fpc-devel] problem with "is" operator

Tom Verhoeff T.Verhoeff at tue.nl
Mon Apr 18 10:29:08 CEST 2005


On Sun, Apr 17, 2005 at 12:01:36PM -0700, Linuxer Wang wrote:
> 
> Can anybody tell me how can I know which specific type an instance of
> class is? The "is" operator seems weird when interface is used.
> 
> Suppose TMyInterface is a interface, and classes TCircle and TSquar
> both implements TMyInterface, and inst:TMyInterface, inst :=

It does not seem right to declare var inst: TMyInterface if you
want inst to have circles and squares as values.  I would expect
that you also have a class TFigure, of which TCircle and TSquare
both are descendants.  These could also implement TMyInterface.

You then declare var inst: TFigure and can do inst := TCircle.Create.

> TCircle.Create. How can I know which instance is inst? The following
> code can not even compile:
> 
> if inst is TCircle then ...
> 
> class type expected, but got "TMyInterface"
> Incompatible type for arg no. 2: Got "TMyInterface", expected "TObject"

The type of inst needs to be a class type (such as TFigure above), not
an interface object type.  If you do as I suggest above, it should work.

	Tom
-- 
E-MAIL: T.Verhoeff @ TUE.NL     | Fac. of Math. & Computing Science
PHONE:  +31 40 247 41 25        | Eindhoven University of Technology
FAX:    +31 40 247 54 04        | PO Box 513, NL-5600 MB Eindhoven
http://www.win.tue.nl/~wstomv/  | The Netherlands




More information about the fpc-devel mailing list