l = LinkedList() l: add(1) l: add(2) l: add(3) var sum = 0 foreach n in l { sum = sum + n } println(">>> " + sum) } constructors as functions calling an instance method
augment java.io.InputStream { function readFully = |this| { let streamReader = java.io.InputStreamReader(this) let reader = java.io.BufferedReader(streamReader) let buffer = java.lang.StringBuilder() var line = 0 while line isnt null { line = reader: readLine() buffer: append(line orIfNull "") } reader: close() return buffer: toString() } }
add("larry", HashMap(): add("email", "[email protected]")) # MrBean and Larry let mrbean = contacts: get("mrbean") let larry = contacts: get("larry") # Illustrates orIfNull println(mrbean: get("url") orIfNull "n/a") println(larry: get("url") orIfNull "n/a") # Querying a non-existent data model because there is no 'address' entry println(mrbean: get("address")?: street()?: number() orIfNull "n/a") dealing with null
then "an email?" when item: startsWith("+33") then "a French phone number?" when item: startsWith("http://") then "a website URL?" otherwise "I have no clue, mate!" } let func = what: andThen(|str| -> println(">>> " + str)) let data = ["http://golo-lang.org/", "+33.6.11.22.33", "[email protected]", "def foo = bar(_._) with :> T"] foreach item in data { func(item) }
worse than reflection (e.g., Groovy-indy) Memory impact of many call sites? JVM support for boxing elimination? Faster vtable helpers for instance-based dispatch?