5 ms·
I don't think it has much to do with type inference. It's more that type systems became more complicated, and so did type names. And with a long composite type
by int_19h 8y ago
I don't think it has much to do with type inference. It's more that type systems became more complicated, and so did type names. And with a long composite type name, the name of the variable gets pushed too far out and obscured. It worked great in Algol, and still works pretty well in C (although that is partly because it splits the declarator to keep array and function syntax to the right of the variable name), but in C++ with templates it's already hard to read.
There are also a variety of issues with parsing it that way, most of which go away entirely if the name is first.
- waterhouse 8y agoRob Pike wrote a rationale for Go's type syntax here: https://blog.golang.org/gos-declaration-syntax https://blog.golang.org/gos-declaration-syntax He seems to agree with you. "One merit of this left-to-right style is how well it works as the types become more complex." ... "Overall, though, we believe Go's type syntax is easier to understand than C's, especially when things get complicated." ... "Go's declarations read left to right. It's been pointed out that C's read in a spiral! See The "Clockwise/Spiral Rule" by David Anderson."
- theoh 8y agoPreviously: https://news.ycombinator.com/item?id=12775735 https://news.ycombinator.com/item?id=12775735 Apparently the Spiral Rule is not entirely correct.
- mehrdadn 8y agoIn C the type of a variable is "variable-centric" rather than "type-centric": it's denoted by syntax akin to the one you would use to use the variable. So if you have int (*arr)[2] that means "if you dereference arr, the expression represents an array of 2 elements". So, for example, if you want a pointer to a function that returns a pointer to an array of two elements, how do you denote it? You'd go step-by-step based on how you would use it: int a[2] int (*f)[2] int (*(*fp)())[2] Once you understand this, the typing syntax should make sense, and you see that any description like spirals or whatever misses the big picture.
- burfog 8y agoSo how do I declare a function pointer for a function that takes two arguments and returns a value, all of which are function pointers of that same type?
- mehrdadn 8y agoI can't tell if this is a rhetorical question or not, but you can't have recursive typedefs in C.
- burfog 8y agoI can't tell either. :-) The grammar seems to prevent recursive typedefs, but I've never seen it spelled out that you can't have them. This has caused me trouble on occasion. I can't even seem to solve this with gcc's non-standard __typeof__ extension. I was halfway expecting to crash or hang the compiler.
- mehrdadn 8y agoI mean if you take the grammar to be part of the official language specification and you believe it prohibits recursive typedefs then isn't that sufficient? It seems a bit like saying it causes you trouble on occasion that they never explicitly spelled out that 'deftype' isn't a synonym for 'typedef'? I would've assumed that if something is not valid in the grammar then it's not valid in the language described by it.
- burfog 8y agoWell the hope is that somehow I'm misunderstanding the grammar... because there simply must be a way to do this. How can the language committee leave this flaw unfixed?
- mehrdadn 8y ago> there simply must be a way to do this. Why? This seems completely baseless. > How can the language committee leave this flaw unfixed? Same way many languages like C++, C#, Java, VB etc. left this "flaw" unfixed.
- jstimpfle 8y ago> especially when things get complicated I think that's a strange rationale and it doesn't fit the philosophy promoted by golang otherwise, which is that we should optimize for the 80% or 90% cases.
- saagarjha 8y agoI am still of the opinion that not putting some sort of visual break between a variable name and the type is somewhat unpleasant. Almost every other language uses a colon to make these easier to visually parse.
- 0815test 8y agoWell, the parsing issues go away mostly because the actual pattern is "keyword on the left, type on the right". `let a: My_Type` is a whole lot easier to parse than `My_Type a`, and this goes even further when instead of My_Type you have something more complex, due to the C-family syntax trick of having "declaration mimic use".
- int_19h 8y ago`a: My_Type` or even `a My_Type` (assuming that juxtaposition is not used for something else) is easier to parse, since you only need one token of lookahead here to know that it's a declaration, and that the rest is a type constructor - and this is true regardless of how complicated that type constructor is. For an extreme example on the other end of the spectrum, in C++, for something like this: a<>::b<c>d; It's impossible to even say whether it's an expression statement or a variable declaration, because it depends on what exactly b is. In fact, it might be impossible to determine even if you have the definition of b in pure C++. Consider: template<size_t N = sizeof(void*)> struct a; template<> struct a<4> { enum { b }; }; template<> struct a<8> { template<int> struct b {}; }; enum { c, d }; int main() { a<>::b<c>d; } Now whether it's a declaration or an expression depends on pointer size used by your compiler. Needless to say, this is all very fun for tools that have to make sense of code, like IDEs. And I think that's another vector for a different syntax - as "smart" tooling (code completion etc) became more common, PL designers have to accommodate that, as well. C++ helped by first becoming popular, and then teaching several painful lessons in that department.