Page 1 of 1

introspection

Posted: 20 Mar 2021 15:02
by jarnold30
Hi, I'm trying to find out how to do introspection.
E.g. for something that is a supertype, how do I find out what subtype it was created as ?

Thanks

Re: introspection

Posted: 21 Mar 2021 19:52
by Thomas Linder Puls
I m not sure I understand what you mean, perhaps you could give more details?

Re: introspection

Posted: 24 Mar 2021 15:03
by jarnold30
I mean, is there a way I can ask an object what type it is?

Re: introspection

Posted: 24 Mar 2021 15:39
by Thomas Linder Puls
The build-in predicate typeLibraryOf can give you the typeLibrary of any term. That will give you the type which it has "here"

But it seems you are looking for the construction type and that can unfortunately not be found by reflection/introspection.

But if you have a suspicion about the type you can try to convert it to that type:

Code: Select all

predicates     handleControl : (control Control). clauses     handleControl(Control) :-         if Button = tryConvert(button, Control) then             % It is a a button         else if TextControl = tryConvert(textControl, Control) then             % It is a textControl         ...

Re: introspection

Posted: 26 Mar 2021 16:48
by jarnold30
OK, thanks

Re: introspection

Posted: 28 Mar 2021 10:35
by jarnold30
Hi, I'm still having trouble with this. What's a type library? Is this documented anywhere?

Re: introspection

Posted: 28 Mar 2021 20:28
by Thomas Linder Puls
A typeLibrary is a runtime representation of a type, it is only documented in form of the corresponding interface ($(ProDir)\pfc\reflection\typeLibrary\typeLibrary.i).

Type libraries are used heavily behind the scenes to make polymorphism work. But I doubt that you will need it for anything.

Re: introspection

Posted: 4 Apr 2021 9:41
by jarnold30
OK, so if I have an object "jim" declared to be of class "fred", I can say

typeLibrary::typeLibraryOf(jim).name()

to get the class name, "fred"?

Re: introspection

Posted: 4 Apr 2021 13:34
by Thomas Linder Puls
Considering what I think you ask the short answer is no.

Assume this:

Code: Select all

interface window ... end interface window   interface control supports window ... end interface control   class textControl : control ... end class textControl
We have a class textControl which constructs objects of the interface type control. The interface type control supports the interface type window, so all controls are also windows, but windows does not need to be controls.

control is the construction type of the class textControl (because textControl constructs such objects).

window and control are types, but textControl is not a type.

Consider this code:

Code: Select all

clauses     run() :-         TC = textControl::new(),         TCN = typeLibrary::typeLibraryOf(TC).name(),         stdio::writef("TCN = %\n", TCN),         useAsWindow(TC).   class predicates     useAsWindow: (window W). clauses     useAsWindow(W) :-         WN = typeLibrary::typeLibraryOf(W).name(),         stdio::writef("WN = %\n", WN),         if C = tryConvert(control, W) then             CN = typeLibrary::typeLibraryOf(C).name(),             stdio::writef("CN = %\n", CN)         end if.
We create a textControl it has type control, so the type library of TC will correspond to control and TCN will get the value "control". When we call useAsWindow we exploit that a control is also a window. The type library of W will correspond to window and WN will get the value "window".

In the if-statement we try to convert the window W to a control, and in this case it will succeed because our object is (also) a control. Subsequently CN will become "control".

What you get in all cases is the view-type and what you ask for (as far as I understand) is the construction-type (which is an interface not a class).

Clearly, the runtime system knows whether the window is a control or not, but that information is not exposed for reflection/introspection.