Skip to content

Instantly share code, notes, and snippets.

@nddrylliog
Created January 13, 2014 12:27
Show Gist options
  • Save nddrylliog/8399459 to your computer and use it in GitHub Desktop.
Save nddrylliog/8399459 to your computer and use it in GitHub Desktop.
Type coercion via callback
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