I'm trying my hand at getting back into some C# coding and found that apparently the language now has var. My first thought was why would a professional, statically typed language have var. Checking out the page I linked to in the submission, you can also do this cool if not slightly funky looking thing where it looks like an SQL predicate just reading from an ordinary array.
> My first thought was why would a professional, statically typed language have var.
It might be because C++ has auto. Kind of related Java has Object.
I haven't figured out if people want languages like C++ to be a loose typed language and just be able to type cast anything to a single type. Boost does introduce the variant type - but it's usage seems very unintuitive. I kind of get what they were trying to do - but overall Boost libraries seem overly complex for what they are trying to solve.
I wrote my own variant type based on Ptype's variant [1] [2]. To me a variant shouldn't just be a container for any object - it should have some basic intelligence for built in types (like what to do when adding a string and an int - the result should be a string. Conversely - when adding an int and a string the result should be an int.).
The 'cool but funky' thing is LINQ. Language Integrated Query. It can be used to query in-memory lists and arrays as well as build SQL to query a DB, or even query XML files.
It is C#'s support for monads.
In terms of var. It's best not to think of it as dynamic like javascript. You can't do this for example:
var a = 123;
a = "Hello";
The second line will throw an exception because 'a' is not a string, it's an int. So it's not dynamic like JS. It simply infers the type when it can so that you don't have to type it, otherwise its exactly the same.
Interestingly C# does have a dynamic aspect too, and that's with the 'dynamic' keyword.
dynamic x = 123;
x = "Hello";
That will work where the var example wouldn't. It's a rarely used feature, but comes in useful to avoid boilerplate when dealing with external 'stuff', like XML files, JSON, or REST responses.