Metaobjects are compile-time objects that can change the compilation process. We show on example of each kind of metaobject:
101bin
is a literal number, it generates5
, it could even add methods to the current prototype, if necessary (it would not make sense, but it could)r"a*bc?"
is a literal string, it generates an object ofRegExpr
, a regular expression objectChar@letter
is a type whose variables can only contain letters. A compile-time or runtime error is issued otherwiseassert
, a macro, is also a metaobject@doc{* this prototype ... *}
, the so-called at-metaobjects. In this example, it takes an attached text, the documentation of the prototype it is attached to@cyan
, a Codeg (visual metaobject)
Metaobjects can:
- add statements to methods, add fields and methods to prototypes
- change the type system (to a limited extend)
- do additional checks in the code, and
- issue compilation errors.
Metaobjects are used for several goals:
- document the code
- generate boilerplate code (including test cases)
- add annotations to declarations (fields, prototypes, etc)
- do additional type system checks
- verify the style of programs
- give information on the code (as the current line number, for example)
- create virtual fields and methods (created when accessed or just simulating their existence)
- evaluate Cyan code at compile-time (in fact, statements only)
- implement Domain Specific Languages (DSLs)
- demand that a prototype is read-only
- create constructs
- create get and set methods
- execute code when a prototype is inherited
- choose a color visually, and much more.
All of this at compile-time. See the more than 90 metaobjects already implemented here. The visual metaobjects, Codegs, are here.