Under what conditions is the unit type?

Before this is noted as a duplicate: I know that this question is related to various questions about compilation errors when using unit as a type argument. Some examples:

They all face a problem like this:

type Interface<'a> = 
    abstract member MyFunc : unit -> 'a

let implementingInstance =
  { new Interface<_> with
        member __.MyFunc () = () } // Compiler error!

From what I understand, the code does not compile, because the unit return function compiles with return voidinternally, which is an additional CLI feature, not a type.

However! The following seems to suit the compiler:

type RecordVersion<'a> =
  { MyFunc : unit -> 'a }

let recordInstance =
  { MyFunc = ignore }

, ignore lambda let.

. ( F #, , .)

API-, . , . , . , F # .

? API, ? ( , , .)

+4
2

, , , unit, .NET- void .NET( , , ). , void, , true unit CLR.

- , unit CLR ( CLR unit void). , , , , , , unit ( unit ), , , .

, F # . (, T<'unit>) Unchecked.defaultof<'unit> (), . T T<unit>, .

+3

, , ( AFAIK ) F # IL-, void. Microsoft.FSharp.Core.FSharpFunc<unit, unit> (aka unit -> unit), "" - void.

, . :

type Interface<'a> = 
    // Note the parentheses to make this member a property rather than a method
    abstract member MyFunc : (unit -> 'a)

let implementingInstance =
    { new Interface<_> with
        // Must use an explicit `fun () ->` instead of a method arg list
        member __.MyFunc = fun () -> () }

, , , (()) (.. unit, ). , :/

+3

Source: https://habr.com/ru/post/1659050/


All Articles