What if D would require * for reference types?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 18 09:12:06 PST 2010


On Mon, 18 Jan 2010 11:17:53 -0500, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> On 2010-01-18 10:08:34 -0500, "Denis Koroskin" <2korden at gmail.com> said:
>
>> It's as simple as that: require '*' to denote reference type. What does  
>> it  give?
>
> It works like that in Objective-C where object variables must always be  
> pointer to objects (enforced by the compiler), and it's not so bad. It's  
> undoubtedly cleaner to read without '*', but as you illustrate it cause  
> issues.
>
>> 3) No more issues with tail-const, tail-shared, tail-immutable;  
>> deprecate  Rebindable (this one was recently discussed):
>
> That's the issue that bothers me most about the current syntax.  
> Rebindable is a nice trick, but it's a hackish solution thrown at a  
> syntactic problem. And it's not so rare either when you're working with  
> immutable objects. It'd be much better if the syntactic problem didn't  
> exist in the first place. Compare this
>
> 	Rebindable!(immutable Object) object;
>
> to this:
>
> 	immutable(Object)* object;
>
> The second is much easier to read. By getting rid of this template  
> trickery playing with the type system and replacing implicit references  
> with the almost identical concept of pointer, I think we would make the  
> language easier to grasp.
>
> Are we too late in D2 development to make this change? I'm in support of  
> it.

One of the biggest problems with C++ class references is the stupid ->  
operator.  I think D has avoided that in the best way possible.  I think  
probably this change wouldn't be too bad, especially given how auto works:

auto c = new C(); // works for both explicit and implicit reference style

The one thing I would insist is that classes cannot be allocated on the  
stack unless explicitly created via scope, and even then a variable can  
never be of class type without reference:

C c; // error, cannot declare a variable of type C.
scope C c; // error, cannot declare a variable of type C.
scope C* c = new C(); // ok, allocated on stack and c works just like a  
normal C reference
scope c = new C(); // equivalent to above line.

This would leave the slicing problem solved the same way it is now -- you  
can stack allocate but only when you specifically request it.

And if you can never declare a variable of class type without denoting it  
is a reference, then the only place code is affected is declarations where  
auto is not involved.

I think we are too late for D2, the book is pretty much finished except  
for the concurrency chapter.  It is a great idea though, I would have  
loved to see this happen before D2 was released.  Maybe D3 can have this  
change.

-Steve



More information about the Digitalmars-d mailing list