Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On an accessibility compilation error when a public type implements an internal interface in F#

Tags:

f#

I wrote some code like

type internal IMyInterface =
    abstract member Method1 : unit -> unit

type Class1() = 

    member this.Y() =
        (this :> IMyInterface).Method1()

    interface IMyInterface with
        member this.Method1() = ()

Note that the public type Class1 implements an internal interface IMyInterface, it compiles fine. In the generated MSIL, "Method1" was shown as private. This is similar to explicit interfaces in C#.

However, when I change the code a bit to

type internal Foo() =
    member x.Value = "Foo"

type internal IMyInterface =
    abstract member Method1 : Foo -> unit

type Class1() = 

    member this.Y() =
        let v = Foo()
        (this :> IMyInterface).Method1(v)

    interface IMyInterface with
        member this.Method1(v : Foo) = ()

This type the interface method "Method1" takes an internal type "Foo" as parameter. This time, it does not compile with an error

The type 'Foo' is less accessible than the value, member or type 'override Class1.Method1 : v:Foo -> unit' it is used in

I have trouble to decipher this error message and find a fix for it. In C#, I can write the following code that compiles fine

internal class Foo
{
    string Value { get; set; }
}

internal interface IMyInterface
{
    void Method1(Foo v);
}

public class Class1 : IMyInterface
{
    public void Y()
    {
        var v = new Foo();
        (this as IMyInterface).Method1(v);
    }

    void IMyInterface.Method1(Foo v)
    {
        throw new NotImplementedException();
    }
}

Any idea about the F# compiler error and how to work around it?

BTW: this may not be the right way/pattern to use interface anyway, I'm just curious on the language syntax

like image 303
Lost In Translation Avatar asked Jul 23 '15 21:07

Lost In Translation


1 Answers

As it was pointed out at Patrick's comment, this issue was logged as a bug of Visual F#. Currently it's included in F# 4.0 Update 3 milestone.

like image 101
Tomasz Maczyński Avatar answered Oct 16 '22 09:10

Tomasz Maczyński