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.
So for example if I have thi
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}
The second example doesn't compile any more:
// Not F#: let myvar2 = {MyRecord with field1 = 1 and field2 = 2}
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)
Edit: 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
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.)
why do I get a compiler warning when implementing 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 ... }
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...
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.
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.