什么时候需要使用new初始化F#类型? [英] When does an F# type need to be initialised using new?

查看:118
本文介绍了什么时候需要使用new初始化F#类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个类,例如:

type MyClass() =
    member this.Greet(x) = printfn "Hello %s" x

是否适合使用

let x = new MyClass()

或没有 new

此外,何时使用新的构造函数比带有提供给类型定义的参数的 do 绑定更有用?

Also, when is the use of a new constructor more useful than a do binding with parameters supplied to the type definition?

推荐答案

我在F#中使用new的模式仅在该类型实现 IDisposable 时才这样做。编译器在这种情况下会出现特殊情况,如果省略 new ,则会发出警告。

My pattern in F# for using new is to only do so when the type implements IDisposable. The compiler special cases this use and emits a warning if new is omitted.

因此,在您的情况下,我不会使用 new 。但是使用以下代码,我会

So in your case I would not use new. But with the following I would

type OtherClass() =
  ...
  interface System.IDisposable with 
    member this.Dispose() = ...

let x = new OtherClass()

这篇关于什么时候需要使用new初始化F#类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆