lang: ast, parser, interfaces: Implementation of nested class sugar
This implements a new type of syntactic sugar for the common pattern of
a base class which returns a child class, and so on. Instead of needing
to repeatedly indent the child classes, we can instead prefix them at
the definition site (where created with the class keyword) with the name
of the parent class, followed by a colon, to get the desired embedded
sugar.
For example, instead of writing:
class base() {
class inner() {
class deepest() {
}
}
}
You can instead write:
class base() {
}
class base:inner() {
}
class base:inner:deepest() {
}
Of course, you can only access any of the inner classes by first
including (with the include keyword) a parent class, and then
subsequently including the inner one.
This commit is contained in:
36
examples/lang/class-include-nested1.mcl
Normal file
36
examples/lang/class-include-nested1.mcl
Normal file
@@ -0,0 +1,36 @@
|
||||
$top = "top-level"
|
||||
class base($s) {
|
||||
test "middle " + $s {}
|
||||
$middle = "inside base"
|
||||
}
|
||||
|
||||
# syntactic sugar for the equivalent of defining a class `inner` inside of base.
|
||||
class base:inner1($s) {
|
||||
test "inner1 " + $s {}
|
||||
|
||||
$last = "i am inner1 and i can see " + $middle
|
||||
}
|
||||
|
||||
class base:inner2($s) {
|
||||
test "inner2 " + $s {}
|
||||
|
||||
$last = "i am inner2 and i can see " + $middle
|
||||
}
|
||||
|
||||
# three deep!
|
||||
class base:inner1:deep($s, $b) {
|
||||
test "deep is " + $s {}
|
||||
|
||||
$end = "i am deep and i can see " + $middle + " and last says " + $last
|
||||
}
|
||||
|
||||
include base("world") as b0
|
||||
include b0.inner1("hello") as b1 # inner comes out of `base`
|
||||
include b0.inner2("hello") as b2 # inner comes out of `base`
|
||||
include b1.deep("deep", true) as d # deep comes out of `inner1`
|
||||
|
||||
test $top {}
|
||||
test $b0.middle {}
|
||||
test $b1.last {}
|
||||
test $b2.last {}
|
||||
test $d.end {}
|
||||
Reference in New Issue
Block a user