Is it possible to write ObjectScript method, which will create new class in namespace and compile it? I mean programmatically create new class and store it. If so, can I edit this class using ObjectScript later(and recompile)?
Reason: I have class structure defined in string variable and I need to add new class to namespace according this string.
Yes, it is. You likely want to make use of
%Dictionary.ClassDefinition
and the related%Dictionary.*Definition
classes (especially%Dictionary.PropertyDefinition
,%Dictionary.MethodDefinition
and%Dictionary.IndexDefinition
) to create and/or modify your class. Provided your string contains some reasonable representation of the data, you should be able to create the class this way.The actual class documentation is available at http://docs.intersystems.com/cache20141/csp/documatic/%25CSP.Documatic.cls?CLASSNAME=%25Dictionary.ClassDefinition
You can then compile the class by calling
$system.OBJ.Compile("YourPackage.YourClass","ck")
.(Note: If your string contains the exported XML definition of the class, you could also write the XML representation to a stream and then call
$system.OBJ.LoadStream()
to import the XML definition. I would only recommend this if you have an exported class definition to start with.)Nothing is impossible. Everything in Caché can be created programmatically. And, Classes is not a execution. There are at least two ways to do it:
All of definition of any classes defined in other classes. Which you can find in package %Dictionary.
The class itself defined in %Dictionary.ClassDefinition. Which have some properties, for defining any parts of classes. So, this is a simple code which create some class, with one property.
And in latest versions, there is one more way for create/change class. If you have text of class as you can see it in Studio. Then, you can load it in Caché, with class %Compiler.UDL.TextServices