fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
341 stars 20 forks source link

Allow inref-argument members to satisfy SRTP constraints #912

Open dsyme opened 3 years ago

dsyme commented 3 years ago

Transferring https://github.com/dotnet/fsharp/issues/9018

This is related to FS-1053: voidptr, IsReadOnly structs, inref, outref, ByRefLike structs, byref extension members

In C#, you can mark the parameters of an overloaded operator as inref:

public readonly struct C
{
    public readonly int I {get;}
    public C(int i)
    {
        I = i;
    }

    public void M()
    {
        var c1 = new C(1);
        var c2 = new C(2);

        var c3 = c1 + c2;
    }

    public static C operator+ (in C a, in C b)
    {
        return new C(a.I + b.I);
    }
}

This operator is not directly usable in F#:

    let c1 = C(1)
    let c2 = C(1)
    let c3 = c1 + c2;

error FS0001: Type constraint mismatch. The type  'C' is not compatible with type 'inref' error FS0043: Type constraint mismatch. The type  'C' is not compatible with type 'inref'

As a workaround, you can explicitly call op_Addition:

let c3 = C.op_Addition(&c1, &c2)

For the first part, this is by design as things currently stand.

We could consider a language suggestion where members taking inref arguments can be considered to satisfy such constraints but it would need an RFC. I'm not sure of its technical feasiblity.

abelbraaksma commented 1 month ago

I think it makes sense to allow this, if indeed feasible. I wonder if it would also solve issues like #1360 (possibly using inline).