Metaprogramming
Raku makes it very easy to do metaprogramming. It is a basic goal of the language.
It is trivial to add a new operator. Most Raku operators are written as normal multiple-dispatch functions in the setting (known as a "prelude" in other languages, but in Raku the setting is a lexical scope notionally surrounding your compilation unit).
There is no a built in factorial operator Raku. It was purposely left out to use as a demonstration of how easy it is to add it. :-)
You may augment a base class with a new method, as long as you declare that you are going to cheat.
Here we add a new method to do natural sorting to the base class Any
. (List
and Array
are both subclasses of Any)
Prints
Grammar mixins work in Raku because grammar rules are just methods in grammar classes, and Raku automatically writes a JIT lexer for you whenever you derive a new language. This functionality already works internally in the standard parser—what is not yet implemented is the augment slang
hook to allow user code to do this mixin. Raku itself is already parsed using such grammar mixins to provide extensible quoting and regex syntax. For example, every time you pick your own quote characters, you're actually deriving a new Raku dialect that supports those start and stop characters. Likewise any switches to impose single or double-quote semantics, or heredoc semantics, is merely a grammar mixin on the basic Q
language.
Last updated