Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F#: Why aren't option types compatible with nullable types?

Tags:

null

f#

Why aren't option types like "int option" compatible with nullable types like "Nullable"?

I assume there is some semantic reason for the difference, but I can't figure what that is.

An option in F# is used when a value may or may not exist. An option has an underlying type and may either hold a value of that type or it may not have a value.

http://msdn.microsoft.com/en-us/library/dd233245%28VS.100%29.aspx

That sure sounds like the Nullable structure.

like image 365
Jonathan Allen Avatar asked Jun 03 '09 20:06

Jonathan Allen


3 Answers

Because of the runtime representation choice for System.Nullable<'T>.

Nullable tries to represent the absent of values by the null pointer, and present values by pointers to those values.

(new System.Nullable<int>() :> obj) = null
|> printfn "%b" // true

(new System.Nullable<int>(1) :> obj).GetType().Name
|> printfn "%s" // Int32

Now consider strings. Unfortunately, strings are nullable. So this is valid:

null : string

But now a null runtime value is ambiguous - it can refer to either the absence of a value or a presence of a null value. For this reason, .NET does not allow constructing a System.Nullable<string>.

Contrast this with:

(Some (null : string) :> obj).GetType().Name
|> printfn "%s" // Option`1

That being said, one can define a bijection:

let optionOfNullable (a : System.Nullable<'T>) = 
    if a.HasValue then
        Some a.Value
    else
        None

let nullableOfOption = function
    | None -> new System.Nullable<_>()
    | Some x -> new System.Nullable<_>(x)

If you observe the types, these functions constrain 'T to be a structure and have a zero-argument constructor. So perhaps F# compiler could expose .NET functions receiving/returning Nullable<'T> by substituting it for an Option<'T where 'T : struct and 'T : (new : unit -> 'T)>, and inserting the conversion functions where necessary..

like image 174
t0yv0 Avatar answered Nov 05 '22 14:11

t0yv0


The two have different semantics. Just to name one, Nullable is an idempotent data constructor that only works on value types, whereas option is a normal generic type. So you can't have a

Nullable<Nullable<int>>

but you can have an

option<option<int>>

Generally, though there are some overlapping scenarios, there are also things you can do with one but not the other.

like image 21
Brian Avatar answered Nov 05 '22 15:11

Brian


Key difference is that must test the option type to see if it has a value. See this question for a good description of its semantics: How does the option type work in F#

like image 1
Robert Avatar answered Nov 05 '22 15:11

Robert