notsojug / jug-material

Material for JUG
The Unlicense
8 stars 2 forks source link

Javaslang's Pattern matching #27

Open civitz opened 7 years ago

civitz commented 7 years ago

Provide a meaningful example of javaslang's structural pattern matching, exploring its benefits.

civitz commented 7 years ago

Key concept: pattern matching in functional languages is total: if an interface can only be implemented by one of N classes, then matching on M<N classes would lead to a partial match, and in functional languages this would trigger a compiler error. In java this is not possible, and slang tries to imitate that via exception (if you use Match.of) or via the use of Option (if you use Match.option) for incomplete matches.