Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

This is something that seems to be settling down in language design. Local variables are usually implicitly typed, and function parameters are usually explicitly typed. This is for readability. "auto" was retrofitted to C/C++ so those languages have implicit typing of locals. C# and Java added "var". Rust uses "let". So this is now pretty standard.

Type info is needed on function signatures mostly so people know how to call the thing. That's why Javascript and Python have acquired bolt-on typing systems. It's also why cross-function type inference hasn't caught on. It can be done technically, but it just confuses the humans.

Humans are not good at maintaining consistency between the thing right here and that other thing way over there. It's best not to design systems which require that.



C already had auto, for automatic storage class specification. It's the default anyway, so I've never seen it used in the wild.


C++ had that too, in 1998. C++11 and C23 changed the meaning of auto to mean "infer this type".


That's helpful, but not entirely true. The old meaning remains in C23 for explicitly specified types, and the `auto` of C23 and C++11 have some notable differences: https://dev.to/pauljlucas/auto-in-c23-ij9


It's not settling down, as C# just changed it up again.

They introduced some new syntaxes in recent versions, c#12 and 10(I think?):

    List<Foo> foos = []; 
    Bar bar = new();
I expect that to replace implicit var declarations as the 'normal' way, especially as that's what VS code analysis suggests now.


I believe 'var' was introduced in C# to make it much easier to work with LINQ, where you can easily return a subset of properties - without var you would have had to declare interfaces. I think ReSharper is responsible for the rapid proliferation of using var everywhere.


Is ReSharper that prevalent? I've always thought it a small subset of programmer's who use it but are very vocal (and preachy). I've only seen it used twice 'in the wild'. And one of those two times most of the devs hated it because it was a performance hog, it was a preachy 'architect' shoving it down their throats.

I don't use resharper, personally found it far too opinionated in what I felt were bad ways. Plus it often resulted in poor performance in VS.

For years I've used var pretty much exclusively, it's just easier than typing the type twice. Even after they introduced new().

With the new array-like declarations I'm considering switching to declaring the type on the left rather than the right.

Which, in the end, is all this syntax is, a pretty way to stop you needing to declare the type twice. And now you can either do it on the right of the expression like this:

    var things = new List<string>();
Or on the left:

    List<string> things = [];
Although, one of the nice things about using var is that it makes it easy for your eye to scan a bunch of variables declared together as they line up.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: