Discussions related to Visual Prolog
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

introspection

Unread post 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
User avatar
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

Re: introspection

Unread post by Thomas Linder Puls »

I m not sure I understand what you mean, perhaps you could give more details?
Regards Thomas Linder Puls
PDC
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

Re: introspection

Unread post by jarnold30 »

I mean, is there a way I can ask an object what type it is?
User avatar
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

Re: introspection

Unread post 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         ...
Regards Thomas Linder Puls
PDC
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

Re: introspection

Unread post by jarnold30 »

OK, thanks
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

Re: introspection

Unread post by jarnold30 »

Hi, I'm still having trouble with this. What's a type library? Is this documented anywhere?
User avatar
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

Re: introspection

Unread post 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.
Regards Thomas Linder Puls
PDC
jarnold30
Posts: 19
Joined: 9 Aug 2020 15:30

Re: introspection

Unread post 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"?
User avatar
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

Re: introspection

Unread post 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.
Regards Thomas Linder Puls
PDC
Post Reply