4 ms·
In Nimrod, when you are declaring a variable with an explicit type, the syntax is: var x, y: int as opposed to the more concise C family way of doing it: int
by xaa 13y ago
In Nimrod, when you are declaring a variable with an explicit type, the syntax is:
var x, y: int
as opposed to the more concise C family way of doing it:
int x, y;
In C, there is no keyword needed to signal that this is a variable declaration. Similarly, in Nimrod, if you declare the return type of a function, you write it in addition to the "proc" keyword.
IMO, the C++0x/D way of declaring a type inferred variable/function, using the auto keyword, is superior, because it is an alternative to specifying the type, not in addition to specifying the type. The Python way, omitting the type entirely, is not really suitable for a statically typed language but at least has the advantage of brevity.
- cma 13y agohttp://en.wikipedia.org/wiki/Most_vexing_parse http://en.wikipedia.org/wiki/Most_vexing_parse
- bjz_ 13y agoIf 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.
- coldtea 13y ago>as opposed to the more concise C family way of doing it The "C family way of doing it" is problematic, and gets crazy complicated with more involved declarations (e.g involving function pointers and such). Nimrods/Go/etc way is much better.