Jump to page: 1 2
Thread overview
double -> double[]... | feature or bug?
Dec 22, 2010
Andrej Mitrovic
Dec 22, 2010
Jonathan M Davis
Dec 22, 2010
Andrej Mitrovic
Dec 22, 2010
Andrej Mitrovic
Dec 23, 2010
spir
Dec 23, 2010
bearophile
Dec 23, 2010
Andrej Mitrovic
Dec 23, 2010
Don
Dec 23, 2010
spir
Dec 24, 2010
bearophile
December 22, 2010
DMD 2.051 with -unittest:

import std.algorithm, std.range, std.stdio : writeln;

void main() {}

void average(double[]) { writeln("non-variadic"); }
void average(double[]...) { writeln("variadic"); }

unittest {
    average(1.5);   // writes "variadic"
}
December 22, 2010
On Wednesday, December 22, 2010 12:05:18 Andrej Mitrovic wrote:
> DMD 2.051 with -unittest:
> 
> import std.algorithm, std.range, std.stdio : writeln;
> 
> void main() {}
> 
> void average(double[]) { writeln("non-variadic"); }
> void average(double[]...) { writeln("variadic"); }
> 
> unittest {
>     average(1.5);   // writes "variadic"
> }

What's the confusion? You gave it a double. That matches the variadic version, no tthe array version. If you gave it an array of doubles, then it would match the array version. That seems entirely straightforward. What's surprising about it?

- Jonathan M Davis
December 22, 2010
I thought the variadic version would only take this type:

average([1.5], [2.5]);

So a variable number of *array* of doubles, not a variable number of doubles.

On 12/22/10, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> On Wednesday, December 22, 2010 12:05:18 Andrej Mitrovic wrote:
>> DMD 2.051 with -unittest:
>>
>> import std.algorithm, std.range, std.stdio : writeln;
>>
>> void main() {}
>>
>> void average(double[]) { writeln("non-variadic"); }
>> void average(double[]...) { writeln("variadic"); }
>>
>> unittest {
>>     average(1.5);   // writes "variadic"
>> }
>
> What's the confusion? You gave it a double. That matches the variadic
> version,
> no tthe array version. If you gave it an array of doubles, then it would
> match
> the array version. That seems entirely straightforward. What's surprising
> about
> it?
>
> - Jonathan M Davis
>
December 22, 2010
On Wed, 22 Dec 2010 15:46:01 -0500, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:

> I thought the variadic version would only take this type:
>
> average([1.5], [2.5]);
>
> So a variable number of *array* of doubles, not a variable number of doubles.

No, that's not it.

T[] arg... is a typesafe variadic argument of type T.  The docs describe it here: http://www.digitalmars.com/d/2.0/function.html (search for typesafe)

Hopefully that helps?

If you were looking for something to accept your above args, it would be:

double[][] arg...

-Steve
December 22, 2010
Oooh. That cought me off guard, sorry.

Thanks Steve.

On 12/22/10, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> On Wed, 22 Dec 2010 15:46:01 -0500, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>
>> I thought the variadic version would only take this type:
>>
>> average([1.5], [2.5]);
>>
>> So a variable number of *array* of doubles, not a variable number of doubles.
>
> No, that's not it.
>
> T[] arg... is a typesafe variadic argument of type T.  The docs describe it here: http://www.digitalmars.com/d/2.0/function.html (search for typesafe)
>
> Hopefully that helps?
>
> If you were looking for something to accept your above args, it would be:
>
> double[][] arg...
>
> -Steve
>
December 23, 2010
On 12/22/10 15:06, Andrej Mitrovic wrote:
> Oooh. That cought me off guard, sorry.
> 
> Thanks Steve.
> 

I'll concede that the syntax can be odd at first, but it also enables some interesting things.  For example, this works:

class Foo {
    this (int i, double f) { /*...*/ }
    /*...*/
}

void someFunc ( Foo foo ... ) { /*...*/ }

someFunc( 5, 3.14 );

Basically, given a class (and I think struct's work as well) as the variadic type, it will accept either an instance of said class, or any combination of values which can be mapped to a constructor of that class.  It can be convenient sometimes.

-- Chris N-S
December 23, 2010
On Thu, 23 Dec 2010 00:34:41 -0600
Christopher Nicholson-Sauls <ibisbasenji@gmail.com> wrote:

> On 12/22/10 15:06, Andrej Mitrovic wrote:
> > Oooh. That cought me off guard, sorry.
> > 
> > Thanks Steve.
> > 
> 
> I'll concede that the syntax can be odd at first, but it also enables some interesting things.  For example, this works:
> 
> class Foo {
>     this (int i, double f) { /*...*/ }
>     /*...*/
> }
> 
> void someFunc ( Foo foo ... ) { /*...*/ }
> 
> someFunc( 5, 3.14 );
> 
> Basically, given a class (and I think struct's work as well) as the variadic type, it will accept either an instance of said class, or any combination of values which can be mapped to a constructor of that class.  It can be convenient sometimes.

While I understand some may consider this a nice feature, for me this is an enormous bug. A great way toward code obfuscation. I like D among other reasons because it's rather clear compared to other languages of the family. There should be no automagic constructor call (at least in this case, when the type is not even mentionned on the caller side).
This may even lead to very naughty bugs. And what if Foo has subtypes, and the one actually invoked is the one intended by the user?

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

December 23, 2010
spir:

> While I understand some may consider this a nice feature, for me this is an enormous bug. A great way toward code obfuscation. I like D among other reasons because it's rather clear compared to other languages of the family.

The main problem here is that I have never felt the need of that feature, so for me it's useless. Has Walter added it after a good number of people have asked for this feature? Has any one here needed it?

Bye,
bearophile
December 23, 2010
On 12/23/10, bearophile <bearophileHUGS@lycos.com> wrote:
> spir:
>
>> While I understand some may consider this a nice feature, for me this is an enormous bug. A great way toward code obfuscation. I like D among other reasons because it's rather clear compared to other languages of the family.
>
> The main problem here is that I have never felt the need of that feature, so for me it's useless. Has Walter added it after a good number of people have asked for this feature? Has any one here needed it?
>
> Bye,
> bearophile
>

You should probably ask in d-general. It looks like a dangerous feature to have. If you have that function laying in another imported module you might not even realize that you're constructing an object. Consider this:

void test(int x, int y, string z);  // your module which you
accidentally forgot to import
void test(int x, Foo f ...);  // some other module that *is* imported
test(1, 4, "def");  // calling site that constructs a Foo object

Maybe it takes more typing, but I prefer this:
test(1, new Foo(4, "def"));

It's explicit and you can reason about it without having to inspect other modules.
December 23, 2010
bearophile wrote:
> spir:
> 
>> While I understand some may consider this a nice feature, for me this is an enormous bug. A great way toward code obfuscation. I like D among other reasons because it's rather clear compared to other languages of the family.
> 
> The main problem here is that I have never felt the need of that feature, so for me it's useless. Has Walter added it after a good number of people have asked for this feature? Has any one here needed it?
> 
> Bye,
> bearophile

I'm almost certain that the behaviour is not intentional.
« First   ‹ Prev
1 2