views:

253

answers:

4

What languages other than C and C++ have explicit reference and pointer type qualifiers? People seem to be easily confused by the right-to-left reading order of types, where char*& is "a reference to a pointer to a character", or a "character-pointer reference"; do any languages with explicit references make use of a left-to-right reading order, such as &*char/ref ptr char? I'm working on a little language project, and legibility is one of my key concerns.

It seems to me that this is one of those questions to which it's easy for a person but hard for a search engine to provide an answer. Thanks in advance!

+4  A: 

Stroustrup is on record as saying the declaration syntax is "an experiment that failed". Unfortunately, C++ has to go along with it for C compatibility. The original idea was that a declaration looked like a use, for example:

char * p;  // declare
* p;       // use (dereference)

but this quickly falls apart for more complex declarations. Many people (at least many Pascal programmers) prefer the syntax:

variable : type;

where type is something like:

array of char

which is about as readable as you are going to get, and is left-to-right.

anon
I think Ritchie later replied that he still stands behind that syntax (don’t have a citation though). Personally, I’m with Stroustrup on this one.
Konrad Rudolph
@Jon Well, it kind of is - char * p can be read left to right.
anon
When C++ has template typedefs, you'll be able to do something like `template<class T> typedef T* ptr_t; template<class T> typedef T ref_t<ptr_t<char>> p;`. I wonder how Stroustrup would feel about that.
Jon Purdy
In my opinion, what destroyed everything are references. So far, even with member function pointers, a declaration mirrored the use pretty well (types => expressions of that type, `::` => `.`). Now with C++0x, rvalue references make it even "worse" :)
Johannes Schaub - litb
Without references, there would not be operator overloading, nor copy constructors.
Seva Alekseyev
Simple declarations are read from right to left, if I'm not mistaken. It gets messy with pointers/references to arrays/functions. But I'm not sure whether having to write a short novel in plain English is a much better alternative: it may be clear but it is some very boring reading.
UncleBens
@UncleBens Please make clear who's comment you are commenting on.
anon
@UncleBens, Messy declarations can be greatly simplified by `identity` though. Like `identity<void(int)>::type *signal(int, identity<void(int)>::type *n);` (signature of `signal` which is written without identity like `void (*signal(int, void(*)(int)))(int);`). And with C++0x alias declarations it becomes `alias<void(int)> *signal(int, alias<void(int)> *);`
Johannes Schaub - litb
A: 

Pascal enforces aliasing of types. You cannot declare:

myvar: ref to array of struct{..pointer to char.}

You have to declare each step in the chain separately in the TYPE block.

Improves readability (if you don't mind some scrolling up and down), probably provides easier parsing, too.

Seva Alekseyev
+1  A: 

In contrast, the approach of Perl 5 is read left-to-right when dereferencing:

$i = 10;  # Scalar
say $i;

$j = \$i;  # Scalar reference
say $$j;

@a = (1, 2, 3);  # Array
$b = \@a;  # Array reference
say $a[0];
say $$b[0];  # prints same thing

But, unlike C/C++, the sigil used against the variable name determines the type that will be read (scalar vs. array vs. hash)

say "Array size: " . scalar(@a);
say "Array size: " . scalar($@b);  # dereference with $, access as @ (array)
spoulson
+2  A: 

I think this is defunct but it might be of use to you: http://en.wikipedia.org/wiki/Significantly_Prettier_and_Easier_C%2B%2B_Syntax

jmucchiello
Wow, it's been a while since I've seen this. It's almost too bad it never caught on. Almost.
Jon Purdy
It overloads `->` with a new meaning that is completely unrelated to the old meaning, and it's supposed to be *more* clear?
caf
@caf- that new meaning only applies to return values in function definitions and IIRC the C++0x standard overrides -> for use in function prototypes as well. See "Alternative function syntax" in the list of new C++ features.
jmucchiello
I can see that, but having the same syntactical element mean something completely different and unrelated still seems like a mistake to me. I would suggest that C++0x should consider carefully using something different (perhaps `=>` ?)
caf
(As an example, people *still* get the comma operator and the comma-that-separates-function-arguments confused!)
caf