我想传递一个类型名称(例如int
, or string
,甚至用户定义类型的名称)作为函数参数。目前我正在做以下事情:
type IntegerOrIntegerList =
| Integer of int
| IntegerList of int list
let f (n : int) (d : 'T) : IntegerOrIntegerList =
match (box d) with
| :? int as i -> Integer(n)
| _ -> IntegerList([0 .. n])
但存在d
以上是偶然的。表达上述逻辑的惯用 F# 方式是什么?
在此先感谢您的帮助。
您可以使用类型 arg:
let f<'T> (n : int) =
if typeof<'T> = typeof<int> then Integer(n)
else IntegerList([0 .. n])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)