The compiler compains about static resolved parameter

Viewed 44
    type Test<'T> =
        static member from : unit -> 'T[] = fun () -> Array.empty


    let inline testFun< ^T, ^S when ^T : (static member from : unit -> ^S [])> () =
        (^T : (static member from : unit -> ^S[]) ())

    let a = testFun<Test<int>, int> ()

I am so confused... Why it says Test< int> does not support the operator "from"?

2 Answers

The SRTP solver can't understand your static method signature, because technically it's not a method, but a property. It's a property whose type is a function unit -> 'T[], but it's not a method. I know, subtle and confusing, but there it is.

If you make it a method, everything will suddenly work:

type Test<'T> =
    static member from () : 'T[] = Array.empty
    type Test<'T> =
        static member from () : 'T array = Array.empty

    type Test1<'T> () =
        static member from : unit -> 'T array = fun () -> Array.empty

    let inline testFun< ^T, ^S when ^T : (static member from : unit -> ^S array)> () =
        (^T : (static member from : unit -> ^S array) ())
    
    let inline testFun2< ^T, ^S when ^T : (static member from : (unit -> ^S array))> () =
        (^T : (static member from : (unit -> ^S  array)) ())

    let a1 = testFun<Test<int>, int> ()

    let a2 = testFun2<Test1<int>,int> ()

For someone if needed... Property should add a pair of quote... omg

Related