Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are new programming languages shifting types to the other side?

If you look at Rust, Go, Swift, TypeScript and a few others, and compare them to C/C++, the first thing that I noticed was how the types have moved positions.

int one = 1;

In comparsion to:

let one:int = 1;

My question: Why?

To me, personally, it is weird reading type specifiers that far into the line, since I am very used to them being on the left. So it interests me on why the type specifiers are being moved - and this not being the case with just one, but many modern/new languages that are on the table.

like image 571
Ingwie Phoenix Avatar asked Mar 13 '16 04:03

Ingwie Phoenix


1 Answers

To me, personally, it is weird reading type specifiers that far into the line, since I am very used to them being on the left

And English is the best language because it is the only language where the words are spoken in the same order I think them. One wonders why anyone speaks French at all, with the words all in the wrong order!

So it interests me on why the type specifiers are being moved - and this not being the case with just one, but many modern/new languages that are on the table.

I note that you ignore the existence of the many older languages which use this pattern. Visual Basic (mid 1990s) immediately comes to mind.

Function F(x As String) As Object

Pascal, 1970s:

var
  Set1 : set of 1..10;

Simply-typed lambda calculus, a programming language invented before computers, in the 1940s:

λx:S.λy:T:S-->T-->S

The whole ML family. I could go on. There are plenty of very old languages that use the types on the right convention.

But we can get far older than the 1940s. When you say in mathematics f : Q --> R, you are putting the name of the function on the left and the type -- a map from Q to R -- on the right. When you say x∈R to indicate that x is a real, you're putting the type on the right. "Type on the right" predates type on the left in C by literally centuries. This is not anything new!

In fact the "types on the left" syntax is the weird one! It just seems natural to you because you happen to have used a language that uses this convention in your formative years.

The types on the right syntax is much superior, for numerous reasons. Just a few:

var x : int = 1;
function y(z : int) : string { ... }    

emphasizes that x is a variable and y is a function. If the type comes to the left and you see int y then you don't know whether it is a function or a variable until later. This makes programs harder for humans to read, which is bad enough. As a compiler developer, let me tell you it is quite inconvenient that the type comes on the left in C#. (I could point out numerous inconsistencies in how C# syntax deals with the positions of types.)

Another reason: In the "type on the right" syntax you can make types optional. If you have

var x : int = 1;

then you can easily say "well, we can infer the int, and so eliminate it"

var x = 1;

but if the int is on the left, then what do you do?

Inverting this: you mention TypeScript. TypeScript is a gradually-typed JavaScript. The convention in JavaScript is already

var x = 1;
function f(y) { }

Given that, plainly it is easier to modify both existing code, and the language as a whole, to introduce optional type elements on the right than it would be to make the "var" and "function" keywords replaced by a type.

Consider also the positioning. When you say:

int x = 1;

then the two things that must be consistent -- the type and the initializer -- are as far apart as they possibly can be. With var x : int = 1; they are side by side. And in

int f() {
   ...
   ...
   return 123;
}

what have we got? The return is logically as far to the right as possible, so why does the function declaration move the type of the return as far to the left as possible?" With the type on the right syntax we have this nice flow:

function f(x : string) : int 
{ ... ... ... return 123; }

What happens in a function call? The flow of the declaration is now the same as the flow of control: the things on the left -- initialization of formal parameters -- happens first, and the things on the right -- production of a return value -- happen last.

I could go on at some additional length pointing out how the C style gets it completely backwards, but it is late. Summing up: first, type on the right is superior in almost every possible way, and second, it is very, very old. New languages which use this convention are the ones that are being consistent with traditional practice.

like image 191
Eric Lippert Avatar answered Sep 20 '22 12:09

Eric Lippert