Created
January 13, 2014 12:27
-
-
Save nddrylliog/8399459 to your computer and use it in GitHub Desktop.
Type coercion via callback
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/Bag | |
coerce: func (s: String, cb: Func <T> (T)) { | |
match s { | |
case "true" => cb(true) | |
case "false" => cb(false) | |
case "0" => cb(0) | |
case => cb(s) | |
} | |
} | |
extend Bag { | |
coerceAdd: func (value: String) { | |
coerce(value, |result| | |
add(result) | |
) | |
} | |
} | |
main: func { | |
b := Bag new() | |
b coerceAdd("true") | |
b coerceAdd("false") | |
b coerceAdd("0") | |
b coerceAdd("jabba the hut") | |
for (i in 0..b size) { | |
"At #{i}, a #{b getClass(i) name}" println() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment