Both syntaxes are equivalent (at least I suppose they are).
let o1 = new Object()
or
let o2 = Object()
Which way do you use more often? What about readability issues?
Both syntaxes are equivalent (at least I suppose they are).
let o1 = new Object()
or
let o2 = Object()
Which way do you use more often? What about readability issues?
I feel like omitting "new" is a bit more functional, so that's my preference. I like that you can treat a constructor just like any other function returning an instance of a type.
They are the same.
I prefer using 'new', with little good reason other than it is what I am accustomed to in other languages, and it makes it easier to findstr/grep for constructor calls (lacking smart tools to 'find all references' in a solution).
I second what kvb says. Additionally, I omit
new
because this allows me to invoke members directly on the constructed object, just like in C#. For example, the follow code works:The following code does not work and results in a compiler error:
So now I need to parenthesize the
new
expression in order to get it to compile:I don't like extra parentheses, so I avoid using
new
now.The one case where I would still use it is when I am creating an object that implements
IDisposable
. This is because if I omit thenew
keyword when creating aIDisposable
the F# compiler gives a warning:Results in this:
I can get rid of the warning by adding in the
new
keyword:The warning also causes me to realize that I should really have a
use
binding for myIDisposable
:But if I wrote my code to use
new
all the time then I would never get the warning reminding me that I am working with anIDisposable
!!!It also makes for less typing. ;-)