Skip to main content
GameDev.net gamedev.net
🔒 Locked

Syntax of Variable Declarations

Started by nolongerhere Nov 25, 2008 at 4:08 PM 2 replies 950+ views
Original Post
nolongerhere
nolongerhere
This is a question about the syntax of popular languages. What really is the difference between...
myVar : integer;
and...
int myVar;
Did Pascal (and im guessing other languages?) have a reason for having the type be on the right side of the identifier? Or is it just a matter of taste and C just did the syntax differently?
pinacolada
pinacolada
Maybe because they allowed for some complex type expressions. Looking at the Wikipedia page, apparently you can write stuff like this:

var  x: 1..10;  y: 'a'..'z';  z: pear..orange;var  set1: set of 1..10;  set2: set of 'a'..'z';  set3: set of pear..orange;


If those expressions were on the left, it might be hard or impossible for the compiler to correctly parse it. Also it would be harder for humans to read.

Another nice thing about the type-on-the-right syntax is that you can make it optional to specify a type. That doesn't apply to Pascal, but I assume that's why Actionscript 3 uses it.
Sneftel
Sneftel
Pascal evolved from ALGOL, yet doesn't use ALGOL's style for variable declarations. Therefore, you can thank Nicklaus Wirth for the rather idiosyncratic convention. My guess is that, as a mathematician, he saw myVar : integer as pleasingly similar to myVar ∈ Z.
Antheus
Antheus
Some history.

Quote:
Two ideas are most characteristic of C among languages of its class: the relationship between arrays and pointers, and the way in which declaration syntax mimics expression syntax. They are also among its most frequently criticized features, and often serve as stumbling blocks to the beginner. In both cases, historical accidents or mistakes have exacerbated their difficulty. The most important of these has been the tolerance of C compilers to errors in type. As should be clear from the history above, C evolved from typeless languages. It did not suddenly appear to its earliest users and developers as an entirely new language with its own rules; instead we continually had to adapt existing programs as the language developed, and make allowance for an existing body of code.


Pascal's syntax is generally much less, or perhaps even completely unambiguous.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.