Hacker News new | ask | show | jobs
by nadams 3954 days ago
> 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.).

[1] https://srchub.org/p/cppvariant/source/tree/tip/variant.h

[2] https://srchub.org/p/ptypes/source/tree/tip/src/pvariant.cxx