小编sti*_*ing的帖子

F#转换为文本运行时提供的类型

换句话说,如果我想要一个完全通用的函数,我如何确保数值类型之间的编译/运行时转换.

例如:

let f x = x + 1 // int -> int
Run Code Online (Sandbox Code Playgroud)

自动假定int因为1.

我目前的黑客攻击是:

let gettype (x:'T) = typeof<'T>  // 'T -> Type
let one x = Convert.ChangeType (1, (gettype x))  // 'a -> obj
let f x = x + one(x) // obj -> obj
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为+没有为objs 定义

f# type-conversion

2
推荐指数
1
解决办法
100
查看次数

标签 统计

f# ×1

type-conversion ×1