Consistency, Templates, Constructors, and D3

F i L witte2008 at gmail.com
Mon Aug 27 05:51:46 PDT 2012


On Monday, 27 August 2012 at 11:15:36 UTC, Chris Nicholson-Sauls 
wrote:
> Before we go proposing something like replacing 'new Foo( val 
> )' with 'Foo.new( val )' ... which is just so Ruby-esque, but 
> that's okay with me ... we need to consider that 'new' is not 
> used only for classes.  Okay, so presumably structs would work 
> the same way, but what of, say, arrays?  What would be the 
> equivalent of 'new int[][]( 5, 10 )' given such a change?
>
> As it stands, 'new' behaves like an operator (behaves like, but 
> is really a grammar artifact) and so is consistent with 
> intuition.  How would we make something like 'int[][].new( 5, 
> 10 )' make sense *without* having to provide a function 
> (presumably through UFCS) for each arity?  And, given the 
> design of D arrays, what would such a function even look like?

idk, I think the int[][].new(5, 10) syntax looks good, and is 
consistent with how I described template parameters. Constructors 
would have completely arbitrary names. So, while a most would 
follow a naming standard (like 'new'), arrays could always use 
something that was a bit more descriptive, like 'alloc':

     struct Point(T)
     {
         T x, y;
         this new(T x, T y) { ... }
     }

     void main()
     {
         // create Point
         auto p = Point(int).new(1, 2);

         // create dynamic array of 5 Points
         // and construct them all
         auto a = Point(int)[].alloc(5);
         a[].new(1, 2);

         // shorthand equivalent
         auto a = Point(int)[].alloc(5).new(1, 2);

         // same thing, but with static array
         auto a = Point(int)[5].new(1, 2);
     }

Personally, I kinda think using 'new' instead of 'alloc' makes 
more sense, for consistency reasons:

     auto a = Point(int)[].new(5).new(1, 2);

but then, I can see why someone would want the distinction so 
it's easier to understand what constructor belongs to the Array.

Either way, I don't see any conflicts with the syntax I purposed.


More information about the Digitalmars-d mailing list