It is sugar, but it is very healthy sugar. You may run into real cast problems
- Do not check
isInstanceOf before calling asInstanceOf - Forgetting that generics don't know their argument type with
isInstanceOf - Does not apply to all cases for the transferred type.
Matching templates handles all this correctly for you: you only get an instance, if it really is, it warns about generics and warns if you have an incomplete match. Thus, in cases where tests and type checks are error prone, pattern matching is reliable and contributes to good design.
source share