开发者

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

开发者 https://www.devze.com 2023-02-10 02:59 出处:网络
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 req

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

精彩评论

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

关注公众号