Metaprogramming in ObjectScript
With $Property
is possible to dynamically get and set values on property of an instanced class and with $ClassMethod
is possible to invoke methods from a class.
I would like to know: Is there an elegant way to do Write
and Set
dynamically on variables without using Xecute
?
Product version: IRIS 2021.2
You can use indirection :
USER>set @field="Henry"
USER>write @field
Henry
USER>write name
Henry
and code quality will struggle with it and dislike it
because you use name in write, but you don't set it explicitly
but don't care much about:
it doesn't understand %-variables and global variable scopes
and $$$macros with code inside, and ... and ... and
Hi Robert,
yes, i never use indirection unless it cannot be done in another way,
(or to show-off what cache object script can do)
There were a good article on the topic a few years ago by @Olga Phomina
Here's the article: https://community.intersystems.com/post/reflection-cach%C3%A9
Not sure if this is what you're getting at, but one of the most exciting and unique things about ObjectScript is how natural it is to do metaprogramming (in the sense of writing ObjectScript that treats other ObjectScript code as data). Because all of your code is in the database, you can work with it from an object or relational perspective - see the %Dictionary package.
There are to main ways of doing this: generators and projections. A generator produces the actual code that will run for a method or trigger in a class at compile time, typically based on the class definition. A projection adds side effects of compiling/recompiling/deleting the class (e.g., updating a global with metadata about the class, creating + queueing compilation of other classes).
isc.rest has a lots of examples of both of these; here's a representative sample:
%pkg.isc.rest.handlerProjection does a bunch of SQL/object updates to data after compilation
%pkg.isc.rest.handler (which uses that projection) has a CodeMode = objectgenerator method that fails compilation of subclasses if certain methods are not overridden.
%pkg.isc.rest.model.action.projection creates a separate class with dependencies on other classes properly declared and queues it to be compiled. The class created extends %pkg.isc.rest.model.action.handler which defines some methods with CodeMode = objectgenerator, though a separate class does the actual work of code generation.
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue