The F # compiler will not prevent you from completely NullReferenceException
. When you work with types defined in .NET, you can still get null
because F # cannot prevent this.
However, when you work with types declared in F #, then the compiler does not allow you to create null
values โโof this type, and therefore it avoids a NullReferenceException
in this case. For example, the following code does not compile:
type Person(name:string) = member x.Name = name // 'Person' is a type declared in F
When you use option<Person>
as an argument, you should explicitly check for None
and Some
cases. This is best done with match
, which checks that you have not missed any of these cases. For instance:
let printName (person:option<Person>) = match person with // You get a warning here, saying that you're not handling the 'None' case! | Some person -> printfn "%s" person.Name
A warning tells you that you must add None
case handling. You can still compile the code, but you will not get a NullReferenceException
when working with F # types unless you ignore the warnings.
See also fooobar.com/questions/3563 / ....
source share