Created
October 25, 2010 01:19
-
-
Save anonymous/644243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import structs/ArrayList | |
// build the house | |
house := CustomHouse new("home", || | |
floor (1, || | |
room ("den") | |
room ("kitchen") | |
) | |
floor (2, || | |
room ("bedroom") | |
room ("batchroom") | |
) | |
) | |
house println() | |
// DSL implementation | |
CustomHouse: class { | |
name: String | |
floors := ArrayList<Floor> new() | |
init: func (=name, f: Method<This>()) { | |
f(this) | |
} | |
floor: func (number: Int, f: Method<Floor>()) { | |
floors add(Floor new(number, f)) | |
} | |
println: func { | |
"House named %s has %d floors" format(name, floors size()) println() | |
for(f in floors) f println() | |
} | |
} | |
Floor: class { | |
number: Int | |
rooms := ArrayList<String> new() | |
init: func (=number, f: Method<This>()) { | |
f(this) | |
} | |
room: func (type: String) { | |
rooms add(type) | |
} | |
println: func { | |
"Floor %d has %d rooms (%s)" format(number, rooms size(), rooms join(", ")) println() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment