double -> double[]... | feature or bug?

Steven Schveighoffer schveiguy at yahoo.com
Thu Dec 23 12:05:14 PST 2010


On Thu, 23 Dec 2010 14:51:02 -0500, spir <denis.spir at gmail.com> wrote:

> On Thu, 23 Dec 2010 13:38:30 -0500
> "Steven Schveighoffer" <schveiguy at yahoo.com> wrote:
>
>> I think it's intentional, and I agree that I've never used or thought
>> "gee, I wish D did this".  I wouldn't be sorry to see it go.  In fact,  
>> I'd
>> advocate for getting rid of it.  It creates a hidden allocation, which  
>> I'm
>> very much against.
>
> Agreed. Not only hidden allocation, but the "feature" may be  
> unintentionally used, leading to hard-to-find bugs. There should never  
> by (user) type instanciation without explicite mention of the type (if  
> only on the left side of an assignment). Surprise --> bugs.

To drive the point home, we can examine the problem that typesafe variadic  
args solves.  Take an array example.  If we didn't have typesafe variadic  
args for arrays, this code:

void foo(int[] args...);

foo(1, 2, 3);

would become:

void foo(int[] args);

int[3] x;
x[0] = 1;
x[1] = 2;
x[2] = 3;
foo(x[]);

Not so nice.  Almost the same issue for a typesafe variadic with  
fixed-sized arrays.

Now, let's look at the same thing for a class:

class C
{
    this(int i, string s);
}

void foo(C arg...);

foo(1, "hi");

And the code without typesafe variadics:

void foo(C arg);

foo(new C(1, "hi"));

Not a lot different, we have just basically shown that you are doing an  
allocation (which is a good thing to show).

Going even further, we can even do this:

void foo(int i, string s)
{
   foo(new C(i, s));
}

And then we get *exactly* the same api.  This feature brings absolutely  
nothing to the table IMO.

-Steve


More information about the Digitalmars-d-learn mailing list