Alex Rivera | Logout

Why do a lot of programming languages put the type *after* the variable name?

Asked 2009-11-11T00:41:48.677
25

I just came across this question in the Go FAQ, and it reminded me of something that's been bugging me for a while. Unfortunately, I don't really see what the answer is getting at.

It seems like almost every non C-like language puts the type after the variable name, like so:

var : int

Just out of sheer curiosity, why is this? Are there advantages to choosing one or the other?

Edit
Report

3 Answers

9

An increasing trend is to not state the type at all, or to optionally state the type. This could be a dynamically typed langauge where there really is no type on the variable, or it could be a statically typed language which infers the type from the context.

If the type is sometimes given and sometimes inferred, then it's easier to read if the optional bit comes afterwards.

There are also trends related to whether a language regards itself as coming from the C school or the functional school or whatever, but these are a waste of time. The languages which improve on their predecessors and are worth learning are the ones that are willing to accept input from all different schools based on merit, not be picky about a feature's heritage.

answered 2009-11-11T01:06:44.937
1

I always thought the way C does it was slightly peculiar: instead of constructing types, the user has to declare them implicitly. It's not just before/after the variable name; in general, you may need to embed the variable name among the type attributes (or, in some usage, to embed an empty space where the name would be if you were actually declaring one).

As a weak form of pattern-matching, it is intelligable to some extent, but it doesn't seem to provide any particular advantages, either. And, trying to write (or read) a function pointer type can easily take you beyond the point of ready intelligability. So overall this aspect of C is a disadvantage, and I'm happy to see that Go has left it behind.

answered 2009-11-16T20:22:51.610
-4

What about dynamically (cheers @wcoenen) typed languages? You just use the variable.

answered 2009-11-11T00:52:44.440

Your Answer