3 ms·
If you have type inference, this is usually not a problem. Also, it is much easier to parse, both for humans and compilers. Rust uses the former way, and it's e
by bjz_ 13y ago
If you have type inference, this is usually not a problem. Also, it is much easier to parse, both for humans and compilers. Rust uses the former way, and it's extremely readable.
Even Herb Sutter, of C++ fame agrees:
> One of the things Go does that I would love C++ to do is a complete left-to-right declaration syntax. That is a good thing because the left-to-right makes you end up in a place where you have no ambiguities, you can read your code in a more strait-forward way, which also makes it more toolable.
(6:00) http://channel9.msdn.com/Shows/Going+Deep/Herb-Sutter-C-Questions-and-Answers http://channel9.msdn.com/Shows/Going+Deep/Herb-Sutter-C-Ques...
- ANTSANTS 13y agoI prefer Pascal-style type declaration as well, but playing Devil's advocate for a moment: There's nothing that says you can't have a left-to-right type declaration syntax and have the variable name appear on the right. So something like this: var ptr_to_array_of_ptr_to_int : *[](*int) would become something like this: *[](*int) ptr_to_array_of_ptr_to_int The former syntax is slightly easier for a program to parse, makes it easier to see variable names, and is more consistent with typical syntax for type inference during variable assignment: var something := some_function(whatever) auto something = some_function(whatever) However, the latter does, as xaa said, eliminate some keystrokes. I also happen to find byte foo = 10 to read more nicely than any of these: var foo : byte = 10 var foo := (u8)10 var foo := 10'u8
- xaa 13y agoGo is a good example. It does variable declaration quite concisely while avoiding parsing ambiguity (although it falls into the same trap as Nimrod on function declaration with its "func" keyword). Type inference can be useful, but important secondary purposes of declaring types, beyond informing the compiler what type a variable is, are as documentation to yourself, and as a sort of compile-time assertion, to ensure that the variable type is indeed what you thought it was. Thus, a language with type inference should not treat manually declared types as some kind of corner case.