replace type check with pattern matching