【发布时间】:2014-05-17 21:10:16
【问题描述】:
我想编写一个从数据流创建对象的函数,例如
let nxL<'T when 'T : (new : unit -> 'T)> (sr:StreamReader) =
let line = sr.ReadLine()
if line <> null then
Some(new 'T(line))
else
None
但是,这不起作用,因为它失败了:
Calls to object constructors on typed parameters cannot be given arguments.
由于构造函数是函数,而 F# 是函数式语言,这对我来说毫无意义。有谁知道如何创建一个将类型作为参数并返回新实例的函数?
【问题讨论】:
-
无法为参数化构造函数指定通用约束。这不仅仅是 F#,一般来说就是 .NET。
-
@MarcinJuraszek:实际上,it is possible 在 F# 中。
标签: f#