3 ms·
These days `Something something = new();` is also allowed.
by Thiez 5y ago
These days `Something something = new();` is also allowed.
- pharmakom 5y agoWhy did they add this when we can already do: var x = new Foo() C# should have fewer, better considered features imo.
- still_grokking 5y agoMaybe because people are still naming their identifiers "x" and such, and "new Foo()" could be some arbitrary expression? Or maybe just because C# is a TIMTOWTDI language? C# looks indeed quite baroque to me. But as I'm not doing any C# I don't know whether this is a good or a bad thing in practice. OTOHS I guess less complexity in a language is generally to be preferred compared to having more flexibility doing the same thing.
- Thiez 5y agoI think it can be useful for initialising properties and fields, where you are not allowed to use `var` (for good reason, if you ask me). But of all the recent features it's the one that I would miss least if it were taken away.
- pharmakom 5y agoFair point! Although I wish they had found a way to add F# style global inference rather all these little tricks
- Thiez 5y agoI suspect type inference becomes pretty tricky in the presence of subtyping. I don't have any F# experience, but I have done some Haskell back in the days and I found the type inference very convenient. But I also think there is something to be said for limiting type inference to "inside" methods where it is effectively invisible to users of a class. Rust has made the same choice: you have type inference inside functions, but never outside.