Why the type is after the identifier?


#1

Hello.
It seems to me that many new programming languages write type annotations in the reverse order (compared to the C order).
That is, some new languages, like Rust, write type annotations the following way:

number: i32

while it is in this order in C-like language:

int number

What is the advantage of the former vs the latter?
Is there any reason to use the reversed syntax?

I know this is a bit off-topic, but I have not found this on the Internet and did not know where to ask this question.
If this kind of question is not appropriate here, could you tell me where to ask this question?

Thanks for your help.


#2

Both orderings are traditional, in different families of programming languages. The v: T form is used in Standard ML and Haskell.

In my opinion, the C notation can be unclear in more complex declarations. Here’s an example taken from Firefox:

JSObject* (*objectMetadataCallback)(JSContext* cx, JSObject* obj);

Quickly, what is the identifier being declared? It’s objectMetadataCallback, but that’s nestled inside other syntax giving a return type and arguments. The Rust analog would be:

objectMetadataCallback:  fn (*mut JSContext, *mut JSObject) -> *mut JSObject;

The type is actually its own phrase, not a context in which the variable being declared is nested. And note that if you take out the mut keywords, they’re about the same length.

I think the C type syntax was motivated by the desire to use the same syntax for expressions as for types: If **x is an int, then clearly x must be a pointer to a pointer to an int. My opinion is that it was an interesting idea, but not a good one in the end.


#3

To add to what @jimb said, it also looks more different in more different cases. Remember, the grammar isn’t

type identifier =

It’s

let pattern: type =

So for example

let (x, y): (i32, i32) =

or

let (a, mut b): (Vec<i32>, String) =  

it’s a bit more complex than just reversing an order.


#4

I’ve no idea, but some related links.

http://blog.golang.org/gos-declaration-syntax

In general, code is more readable and parser is easier to implement .


#5

Thanks for your answer.

I believe we used a keyword (let) because it is easier to parse when there is a pattern, right?
Otherwise, we could have used the Go way (identifier type).

Thanks to everybody else for their answer. They were very useful.