Static extension methods supporting member constraints

Str*_*ger 5 extension-methods f# type-constraints

I need to implement a static extension method supporting member constraints on some basic primitive types like integers, floats, etc. Here's my code for signed integers:

module MyOperators =
    let inline foo (x : ^T) = (^T : (static member Foo : ^T -> int) (x)) 

    type System.Int32 with 
        static member Foo(x : Int32) = 7 // static extension
Run Code Online (Sandbox Code Playgroud)

Test code:

open MyOperators    
let x = foo 5 // x should be 7
Run Code Online (Sandbox Code Playgroud)

But compiler complains with error:

The type 'System.Int32' does not support any operators named 'Foo'

What am I missing here? Thanks!

Bri*_*ian 5

F#中的静态成员约束永远不会找到"扩展方法",它们只能看到类型的内部方法(以及F#语言规范中调用的一些特殊情况).

也许你可以使用方法重载?你的最终目标是什么?