简体   繁体   中英

Implement member that uses type alias

The following works fine:

type T = int
type I =
    abstract member Ok : int
    abstract member Ok2 : T

type C() =
    interface I with
        member this.Ok = 1
        member this.Ok2 = 1

But if the alias is a function rather than a method, it doesn't work:

type T2 = unit -> int
type I2 =
    abstract member Ok : unit -> int
    abstract member Err : T2

type C2() =
    interface I2 with
        member this.Ok () = 1
        member this.Err () = 1 // No interface member found

What am I missing here?

Indeed it works. The problem is the way you're testing it. For instance if you write the last line like this:

member this.Err = fun () -> 1

will work fine and it will give you a hint of what's going on.

Your alias is applied as (unit -> int) with parens, which makes a difference, it will rather create a property containing an F# function value.

Try adding the parens here:

abstract member Ok : (unit -> int)

Then the first method would have to be re-written the same way. So the issue is that when you write abstract member Ok : unit -> int a standard .NET method with no input parameters is created, if you want to keep the F# syntactic unit type in the compiled version you need to add parens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM