|
|
|
|
|
by hannibalhorn
4703 days ago
|
|
The type cast as part of the switch is really cool, I hadn't seen that before. switch v := v.(type) {
case string:
w.Write(int32(len(v)))
w.Write([]byte(v))
default:
w.err = binary.Write(w.w, binary.LittleEndian, v)
}
Great way to alter control flow based on the type, without a ton of ugly casts cluttering things up. |
|
Here is how you would return all leaf values in a binary tree in Scala:
EDIT: Would actually be more idiomatic in Scala to extract the values from the case classes rather than just matching on type...yet another wonderful feature of pattern matching. Coded as follows: