Why inline static type resolution does not work on List?

I'm trying to write an inline map function that can be used with any type that has mapx as a static member with following code:

type Container<'T> =
  { value: 'T }

type container<'a> = Container<'a>

type Container<'T> with
  static member mapx (f: 'T -> 'b,  x: 'T container) = { value=f(x.value) } // tuple form...

type List<'T> with
  static member mapx (f: 'T -> 'b, x: 'T list) = List.map f x

let inline map (f: ^a -> ^b) (x: ^t) =
  (^t: (static member mapx: (^a -> ^b) -> ^t -> ^c ) (f, x))  // (2) curry form

let plus2 = (+) 2

let x = { value=2 } |> map plus2  // ok
let y = [1;2;3] |> map plus2      // (1) Error... 

printfn "%A" x
printfn "%A" y

I have two questions:

(1) This line causes compiler warning, but the previous expression is ok!? The error message is

The type 'int list' does not support operator mapx

Can anyone explain why it does not work on List but does work for Container? and what I have to do to make it work with List?

(2) Am I using the constraint correctly? I find that the constraint is written in curry form but the method has to be in tuple form... this is quite confused.

Thank you!


F# doesn't support statically resolved type constraints for extension members, that's why List doesn't work.

When the compiler can, it will compile extension-syntax members as normal members, and that's why Container works.

链接地址: http://www.djcxy.com/p/80666.html

上一篇: Python是否优化尾递归?

下一篇: 为什么内联静态类型解析在List上不起作用?