Are default links invalid by default in F #?

I know that this is not the case in C #, and it is in languages ​​like Haskell (if I'm not mistaken), so maybe F # had the same semantics by default.

And even if it does not exist in C #, this is a language restriction, not a runtime, right? Like F # or some other new .NET language, it can actually implement this as the default without using any hacks.

+5
source share
2 answers

In F #, if you define a new class or another type in F # code, then it will be invalid by default, in the sense that, for example,

type MyClass() = ...
...
let x : MyClass = null   // does not compile

.NET IL- , .NET, , , , # F #

let x : MyClass = Unchecked.defaultOf<MyClass>

. , " " - .NET, " #, ", ", ". . F # null, F #, interop .NET runtime, . ( .)

+11

, , , F # null, , . AllowNullLiteral:

[<AllowNullLiteral>]
type T1() = class end

type T2() = class end

let t1 : T1 = null
let t2 : T2 = null // compiler error

, , . F # ; , , "" , option:

let myFunc (o:option<T2>) = 
  match o with
  | None -> "No value passed in"
  | Some(t2) -> "A T2 instance was passed in"

NULL #, , ( , ).

+5

All Articles