How to invoke type method in context of class variable
Posted: 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.
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
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>();
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