What if D would require * for reference types?
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Mon Jan 18 10:28:38 PST 2010
Denis Koroskin wrote:
> You know, I'm not usually a guy who proposes radical changes to the
> language. I like the way D is, but there are a few proposals that keep
> popping up once in a while without a good rationale, and this simple
> idea stroke my head solves many of the issues that were found in
> previous proposals, so I decided to share it with you.
>
> It's as simple as that: require '*' to denote reference type. What does
> it give?
>
> 1) Consistency between classes and structs:
>
> Struct* s = new Struct();
> Class* c = new Class();
>
> It allows easier transition between classes and structs (please note
> that I don't propose any changes to class semantics):
> Foo* f = new Foo(); // works for both classes and structs
>
> 2) .sizeof consistency, get rid of __traits(classInstanceSize, Foo)
> (deprecate in favor of Foo.sizeof):
>
> Foo* f = cast(Foo*)malloc(Foo.sizeof); // works for both classes and
> structs (and enums, unions, etc)
> f.__ctor(args);
>
> 3) No more issues with tail-const, tail-shared, tail-immutable;
> deprecate Rebindable (this one was recently discussed):
>
> shared(Foo)* foo; // local pointer to shared type, works for both
> classes and structs
>
> Please note that we get these bonuses by only enforcing '*' to denote
> refence type, not a huge change to compiler IMO. It *will* break
> existing code, but the fix is rather trivial.
>
> To be continued.
As others have mentioned, using the same syntax for pointers and
references may not be such a good idea. That said, I've always wanted a
general way to create references, for instance a "ref" type constructor:
// Compulsory for classes.
class Foo { }
ref(Foo) foo = new Foo;
// Symmetry between classes and structs.
struct Bar { }
ref(Bar) bar = new Bar;
// 'new' *always* returns a reference.
ref(int) i = new int;
-Lars
More information about the Digitalmars-d
mailing list