Christian
Christian

Reputation: 7852

Why is there a Value property on the Option type in F#?

According to the documentation for Option.Value<'T>:

Get the value of a Some option. A NullReferenceException is raised if the option is None.

and

This function is for use by compiled F# code and should not be used directly.

Does the last sentence imply that it's due to interop? What's the use case if so? Otherwise it seems very strange since the whole point of the Option type is to make the possibility of undefined values explicit and encourage handling of them, which easily can be circumvented:

let none : int option = None
let value = none.Value

Upvotes: 3

Views: 197

Answers (1)

John Palmer
John Palmer

Reputation: 25516

I imagine it is there so that

match v with 
|Some(t) -> t

works - without that Value property, you wouldn't be able to get the t with any functions which are available to F# code (Note there are some DU properties which are not accesible from F# which are an alternative here). There may also be some very minor speed benifits if you know that the option is Some as you don't check it if you use value directly

Upvotes: 3

Related Questions