simendsjo
simendsjo

Reputation: 4749

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?

Upvotes: 1

Views: 257

Answers (1)

Gus
Gus

Reputation: 26174

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.

Upvotes: 4

Related Questions