dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.88k stars 783 forks source link

Cannot implement a generic interface with static abstract members with different generic arguments #15713

Open Andreas-Dorfer opened 1 year ago

Andreas-Dorfer commented 1 year ago

I'm experimenting with the new System.Numerics namespace and came across an issue with generic interfaces with static abstract members. I cannot implement a generic interface with static abstract members with different generic arguments. In this example, I want to implement IMultiplyOperators<Vector, double, Vector> and IMultiplyOperators<Vector, Vector, Vector>.

open System.Numerics

type Vector = Vector of x: double * y: double * z: double with

    //factor
    static member (*) (Vector (x, y, z), f) =
        Vector (x * f, y * f, z * f)

    //cross product
    static member (*) (Vector (x1, y1, z1), Vector (x2, y2, z2)) =
        Vector (y1 * z2 - z1 * y2, z1 * x2 - x1 * z2, x1 * y2 - y1 * x2)

    interface IMultiplyOperators<Vector, double, Vector> with
        static member (*) (a, f) = a * f
    interface IMultiplyOperators<Vector, Vector, Vector> with
        static member (*) (a, b) = a * b

I'm getting the following compiler errors:

Program.fs(13,15): error FS0001: This expression was expected to have type    'decimal'    but here has type    'Vector'
Program.fs(13,15): error FS0001: This expression was expected to have type    'decimal'    but here has type    'Vector'
Program.fs(13,15): error FS0909: All implemented interfaces should be declared on the initial declaration of the type

VectorTry.zip

vzarytovskii commented 1 year ago

It is supported for instance members, not sure what's the issue with statics: https://github.com/fsharp/fslang-suggestions/issues/545

edgarfgp commented 11 months ago

@gusty will this also be fixed by https://github.com/dotnet/fsharp/pull/16199 ?

gusty commented 11 months ago

No @edgarfgp , #16199 is about allowing 'interface .. with' within an interface, in this case is a type which is already supported.

Program.fs(13,15): error FS0909: All implemented interfaces should be declared on the initial declaration of the type is already solved in F#8 by #16157

However if I find time I could have a look and see where the issue is, in the compiler.

edgarfgp commented 11 months ago

Thanks for the clarification. Happy to work together on this if do not have much time