开发者

Query on generic usage in F#, specifically new T() and ref cells

What would the F# equivalent of this bit of C#:

    public T GetNewItem()
    {
        return new T();
    }

In addition 开发者_开发知识库how would you return the new T() as a ref cell for usage in a .Net library requiring an out or ref parameter?


I think something as simple as this would work:

let getNewItem() = new 'T()

It infers the default constructor constraint.

let mutable d = getNewItem() //d inferred to be System.DateTime
DateTime.TryParse("1/1/2011", &d)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜