How to invoke type method in context of class variable

For questions and postings not covered by the other forums
Kevin
Posts: 29
Joined: Wed Oct 14, 2009 9:03 am
Location: Christchurch

How to invoke type method in context of class variable

Postby Kevin » Fri Mar 08, 2019 2:23 pm

Hi,

For anyone who's starting to use type methods, here's a trick for invoking type methods in the context of a class variable.

Code: Select all

// With 'anyClass' being a Class variable retrieved or parameter supplied ( anyClass.number.String & ".1" ).asObject().<Type>@<method>();
This works by faking the OID (rather than needing to create an instance), which is then cast to relevant type to call the method we're after.

If you're wondering about the use case, I wanted to invoke type methods in this way as part of iterating through a number of classes to query/retrieve some metadata, where the results may be derived generically by the base implementation or explicitly overridden with re-implementations. Another use case I've got in mind is where I'd like to query a list of form classes to determine which ones apply to a supplied record (currently, we have to create an instance of the form which is expensive due to a thin-client trip).

Everything else attempted failed with an exception, wouldn't compile, or ended up being invoked in the context of the Class type (as method was defined on Object). Anyone else found a better way?

Cheers,
Kevin

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: How to invoke type method in context of class variable

Postby BeeJay » Fri Mar 08, 2019 2:50 pm

Code: Select all

// With 'anyClass' being a Class variable retrieved or parameter supplied ( anyClass.number.String & ".1" ).asObject().<Type>@<method>();
This works by faking the OID (rather than needing to create an instance), which is then cast to relevant type to call the method we're after.
Out of interest, what are you putting in place of <Type> in the above code snippet to cast to the relevant type?

Cheers,
BeeJay.

Kevin
Posts: 29
Joined: Wed Oct 14, 2009 9:03 am
Location: Christchurch

Re: How to invoke type method in context of class variable

Postby Kevin » Fri Mar 08, 2019 2:54 pm

In my case, it was Object as I'd added the type method to that class, but I believe you'd be able to put in any subclass, i.e. Form for the other use case I'd described.

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: How to invoke type method in context of class variable

Postby BeeJay » Fri Mar 08, 2019 3:01 pm

But isn't that effectively the same as just coding:

<Type>@<method>

or in your examples

Object@<method>
Form@<method>

etc...

Or am I missing some nuance around type methods?

Kevin
Posts: 29
Joined: Wed Oct 14, 2009 9:03 am
Location: Christchurch

Re: How to invoke type method in context of class variable

Postby Kevin » Fri Mar 08, 2019 3:06 pm

Using Object@<method> or Form@<method> will always invoke the base implementation on those classes, whereas I want any re-implementations to be invoked when iterating through their sub-classes.

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: How to invoke type method in context of class variable

Postby BeeJay » Fri Mar 08, 2019 5:00 pm

Ah, that's the nuance I was missing. Thanks for the clarification.

Cheers,
BeeJay.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 27 guests