Reputation: 19700
I am wondering if there is a way to pass an Option value along from the matching pattern without recreating it?
var funA x = Some(x)
var funB x =
match funA x with
| Option(y) -> Option(y) // <---- Recreating the Option value. Can I return the very same one?
| _ -> failwith "Whatever"
Upvotes: 1
Views: 89
Reputation: 243061
If you want to pattern match on an option and return it only when it is Some
(and do something else if it is None
), then you can use the as
construct in pattern matching:
let funB x =
match funA x with
| Some _ as optY -> optY
| _ -> failwith "Whatever"
Although this function does not look very useful to me - why return the value as option when you check to make sure that it is Some
and throw exception otherwise? The return type is option 'a
but, in fact, you could always return just 'a
value, so there is no point in using option
...
It is quite likely that there is a nicer solution to your problem - it depends on what exactly you are trying to do. Have a look at the functions in the Option
module - there might be something that captures what you need.
Upvotes: 3