vik*_*ata 1 generics f# inline function
我尝试使用"内联"来定义一个适合不同输入参数类型的函数:
> let x=2.0
- let inline f x=x+1
- f x;;
f x;;
--^
stdin(6,3): error FS0001: This expression was expected to have type
int
but here has type
float
Run Code Online (Sandbox Code Playgroud)
我希望在f上应用"inline"之后,我得到了一个通用函数调用"f".但似乎失败了.怎么纠正呢?
最好的方法就是这样使用genericOne:
let inline f x = x + LanguagePrimitives.GenericOne
Run Code Online (Sandbox Code Playgroud)
这是因为当你使用时1,编译器已经认为函数参数必须是一个,int因为你只能添加ints到其他参数ints
然后你可以用它来调用它
> f 1;;
val it : int = 2
> f 1.0;;
val it : float = 2.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |