How about Go's &Struct instead of new?

Bill Baxter wbaxter at gmail.com
Thu Nov 12 10:27:29 PST 2009


I fear there could be a long parade of these "How about Go's ____"
topics, but anyway, here goes...

In Go (from what I understand), a struct is stack allocated with
   x := Struct();
and heap allocated with
   x := &Struct();

For D that would be
   auto x = Struct(); // stack
   auto x = &Struct(); // heap
And for D classes one could use
   auto x = Class(); // on the heap
   auto x = *Class(); // on the stack
For arrays
   auto x = &float[14];  // heap
   auto x = &float[](10,20)  // heap
   auto x = float[14];  // alt form of fixed size stack array?
For AAs
   auto x = &float[int];  // heap
   auto x = float[int]; // stack

Mostly just a syntax bikeshed, but this seemed like a nice way to
eliminate the "new" syntax that wasn't mentioned previously.
They also kind of round out the declaration possibilities so that all
these built-ins can be declared as auto or as literals. For instance,
a function that needs a 10 element scratch array passed in could be
called like:
       foo(A,B, float[10][]);
equivalent to
       float[10] tmp;
       foo(A,B, tmp[]);

I haven't really understood Dimscha's request for a templatized
constructor, though.  Maybe this won't help there.

--bb



More information about the Digitalmars-d mailing list