f# - How to perform multiple styles of pattern matching? -
just started play f#. terrible i'm now, not know search similar thread too.
this i'm trying do:
let test animal = if animal :? cat //testing type "cat" elif animal :? dog //testing type "dog" elif animal = unicorn //testing value equality "impossible" else "who cares" basically involves type test pattern matching along other conditional checks. can first part (type checking) done this:
let test(animal:animal) = match animal | :? cat cat -> "cat" | :? dog dog -> "cat" | _ -> "who cares" 1. there way can incorporate equality checking (as in first example) in above type test pattern matching?
2. such multiple kinds of checks performed in single pattern matching construct frowned upon in f# circle?
this equivalent using pattern matching:
let test (animal:animal) = match animal | :? cat cat -> "cat" | :? dog dog -> "dog" | _ when animal = unicorn -> "impossible" | _ -> "who cares" i wouldn't frowned upon. it's needed oop , it's better (more concise, clearer) c# equivalent.
Comments
Post a Comment