支持成员约束的静态扩展方法 [英] Static extension methods supporting member 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
测试代码:
open MyOperators
let x = foo 5 // x should be 7
但是编译器抱怨错误:
类型'System.Int32'不 支持任何名为"Foo"的运算符
The type 'System.Int32' does not support any operators named 'Foo'
我在这里想念什么?谢谢!
What am I missing here? Thanks!
推荐答案
F#中的静态成员约束永远找不到扩展方法",它们只能看到类型的内在方法(以及F#语言规范中提到的一些特殊情况) ).
Static member constraints in F# never find 'extension methods', they can only see intrinsic methods on types (and a few special cases called out in the F# language spec).
也许您可以使用方法重载代替?您的最终目标是什么?
Perhaps you can use method overloading instead? What is your ultimate goal?
这篇关于支持成员约束的静态扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!