F#new关键字。它是什么? [英] F# new keyword. What is it for?

查看:183
本文介绍了F#new关键字。它是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有F#类和记录的例子中,我看到一个类通过new关键字或类型名称实例化/创建。

In all examples of F# classes and records I see that records an classes are instantiated/created via new keyword or simply by the type name.

我有这个记录:

type MyRecord = {field1: int; field2:int}
let myvar = {new MyRecord with field1 = 3 and field2 = 3}
let myvar2 = {MyRecord with field1 = 1 and field2 = 2}
let myvar3 = {field1 = 34; field2 = 23}

它们似乎都是一样的。
这个例子也是一样的:

They seem to be all the same. It is the same for this example too:

type MyClass(x: int) =
   let mutable xx = x
let myvar = MyClass(12)
let myvar2 = new MyClassw(234)


b $ b

那么什么是新的? Thnkyou

Well what is new for?? Thnkyou

啊...我知道有关于类的新问题有答案,但没有记录。此外,我不明白为什么新的应该是一个可选的和abosultely无关键的工作时构建类?有可能使用新的或不使用它的类不会改变什么?

Ah... I know that there are answers for the question of new regarding classes, but there is no mention for records. Furthermore, I do not understand why new should be an optional and abosultely indifferent keywork when constructing classes? Is it possible that using new or not using it for classes does change nothing?

推荐答案

new 关键字通常不用于实例化F#记录。第一个例子是记录声明的OCaml-ish语法:

The new keyword is typically not used to instantiate F# records. The first example is the OCaml-ish syntax for record declarations:

let myvar = {new MyRecord with field1 = 3 and field2 = 3}
// Normal F#: let myvar = { MyRecord.field1 = 3; field2 = 3}

第二个例子不再编译:

// Not F#: let myvar2 = {MyRecord with field1 = 1 and field2 = 2}

对于类,你总是可以省略 new 。但是,如果不使用 new 关键字实例化 IDisposable 类,就会收到编译器警告。

For classes, you can always omit new. However, you get a compiler warning if you instantiate an IDisposable class without using the new keyword.

// These two are the same
let myvar = MyClass(12)
let myvar2 = new MyClass(234)

// Compiler warning
let f = FileStream("hello.txt", FileMode.Open)

// No warning
use f = new FileStream("hello.txt", FileMode.Open)

为了回应您的评论 -

In response to your comment -


是不是有一个规则或理由,为什么新是这么灵活?为什么对于类使用或不使用新方法是一样的

isn't there a rule or s rationale why new is so "flexible"? Why is it the same using or not using new for classes

new 可选 - 我不知道使其灵活的理由;这将是很好,如果有一些指导一种方式或其他。 (我的偏好是除非响应编译器警告,否则不要使用 new 。)

new is optional - I'm not aware of a rationale for making it flexible; it would be nice if there was some guidance one way or the other. (My preference is to never use new, except in response to the compiler warning.)


为什么在实现IDisposable时会收到编译器警告

why do I get a compiler warning when implementing IDisposable

因为我从来不使用 new 通常,我认为这是编译器的方式提醒我写使用使用分配 IDisposable

Since I never use new normally, I take this to be the compiler's way of reminding me to write use or using when I allocate an IDisposable.


为什么记录不使用我写的第二种语法之前?

and why records do not use the second syntax I wrote before?

{new MyRecord with ...} 是Haskell语法。它可能在一个F#编译器beta中有效,但它不在F#2.0上解析。 (为什么你认为这应该是有效的?)

{ new MyRecord with ... } is the Haskell syntax. It may have been valid in one of the F# compiler betas, but it doesn't parse on F# 2.0. (Why do you think this ought to be valid?)


我试图找到一般的逻辑规则/指南,一堆或分散的语法规则...

I'm trying to find a general logic rule/guideline rather than learning byheart a bunch or scattered syntax rules...

我知道你的感觉 - F#有点像这样,从C语言,有一个正确的方法和错误的方式做某事。

I know how you feel -- F# is a little like this, particularly when you come from a language like C#, where there's a right way and a wrong way to do something. My advice is to pick one of the alternatives and stick to it.

这可能有助于: F#组件设计指南 [PDF]从Microsoft。这是一套对你的F#代码的建议 - dos和don'ts。

This may help: the F# Component Design guidelines [PDF] from Microsoft. It's a set of advice -- dos and don'ts -- for your F# code.

这篇关于F#new关键字。它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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