|
"the extra syntax in this case is not adding any extra information (to the compiler)." This actually isn't true in C++, because there could be many classes in scope that could take a list initializer like this. In Haskell, no literal with [] can "turn into" something besides a list, but that can happen in C++: vector<int> items = {1,2,3,4};
int[] items = {1,2,3,4};
These have different types, so how would C++ know which one you meant if you instead wrote: auto items = {1,2,3,4};
Again, in Haskell this isn't a problem because literals have essentially a single type. (Edge cases around integers and strings notwithstanding).Edit: Just to clarify, in a Hindley-Milner system you could maybe get away with something like that, but everything you name in an HM system you must use, and that isn't the case in C++: void foo() {
auto items = {1,2,3,4};
return;
}
I can then make two classes with list constructors: struct FooClass {
FooClass(std::initializer_list<int> list) {
cout << "Made a Foo!" << endl;
}
};
struct BarClass {
BarClass(std::initializer_list<int> list) {
format_your_hard_disk();
}
};
Obviously there are consequences to choosing the right type, but the type of that value never leaks out of the function. Nevertheless, because side-effects can happen anywhere, even in a constructor, C++ cannot optimize that out.This might be a convoluted example, and it may be flawed, but conjuring up others is not hard and demonstrates that C++ simply cannot ever have true HM type inferencing. Since the "real deal" is not possible, the language is complex and the standard is large, I would not expect to be able to live without annotations in C++-land. (Again, the FQA makes the horror of multiple non-orthogonal solutions to the same problems quite clear). |
This is part of why I said that the foundations of C++ are too far-gone.
And this syntax isn't necessary to save on typing. In a language that supported syntactic macros (such as Scheme or Racket) we could write something like:
which expands to something like: If you're interested in true syntactic macros for non-sexp languages (though I do suggest getting over the parentheses, my color settings make them nearly indistinguishable from the background) look at Rust [1].Actually, Rust also has type inference [2].
Hell, stop programming in C++ and start using Rust! [3]
[1] http://dl.rust-lang.org/doc/tutorial-macros.html [2] http://dl.rust-lang.org/doc/0.4/rust.html#type-system [3] http://www.rust-lang.org/