Files
mgmt/examples/lang/class-include-nested0.mcl
James Shubin aae0e16350 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.
2024-01-14 17:10:31 -05:00

20 lines
404 B
Plaintext

$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:inner($s) {
test "inner " + $s {}
$last = "i am inner and i can see " + $middle
}
include base("world") as b1
include b1.inner("hello") as b2 # inner comes out of `base`
test $top {}
test $b1.middle {}
test $b2.last {}