我想在代码引用中使用数字文字。
在两个贡献之后
一种方法是:
type FromInt = FromInt with
static member ($) (FromInt, _:sbyte ) = fun (x:int) -> sbyte x
static member ($) (FromInt, _:int16 ) = fun (x:int) -> int16 x
static member ($) (FromInt, _:int32 ) = id
static member ($) (FromInt, _:float ) = fun (x:int) -> float x
static member ($) (FromInt, _:float32 ) = fun (x:int) -> float32 x
static member ($) (FromInt, _:int64 ) = fun (x:int) -> int64 x
static member ($) (FromInt, _:nativeint ) = fun (x:int) -> nativeint x
static member ($) (FromInt, _:byte ) = fun (x:int) -> byte x
static member ($) (FromInt, _:uint16 ) = fun (x:int) -> uint16 x
static member ($) (FromInt, _:char ) = fun (x:int) -> char x
static member ($) (FromInt, _:uint32 ) = fun (x:int) -> uint32 x
static member ($) (FromInt, _:uint64 ) = fun (x:int) -> uint64 x
static member ($) (FromInt, _:unativeint) = fun (x:int) -> unativeint x
static member ($) (FromInt, _:bigint ) = fun (x:int) -> bigint x
static member ($) (FromInt, _:decimal ) = fun (x:int) -> decimal x
static member ($) (FromInt, _:Complex ) = fun (x:int) -> Complex(float x,0.0)
let inline fromInt (a:int) : ^t = (FromInt $ Unchecked.defaultof< ^t>) a
module NumericLiteralG =
[<ReflectedDefinition>]
let inline FromZero() = LanguagePrimitives.GenericZero
[<ReflectedDefinition>]
let inline FromOne() = LanguagePrimitives.GenericOne
[<ReflectedDefinition>]
let inline FromInt32 (i:int) = fromInt i
为每个 ($) 运算符添加 [ < ReflectedDefinition > ] 属性就可以了。但是一旦我将 [ < ReflectedDefinition > ] 添加到
let inline fromInt (a:int) : ^t = (FromInt $ Unchecked.defaultof< ^t>) a
我收到以下编译错误:
“引号不能包含进行成员约束调用的表达式,或使用隐式解析为成员约束调用的运算符”
这是报价的限制吗?有没有其他方法可以得到相同的结果?
非常感谢您的任何想法。