Jump to page: 1 2
Thread overview
Solving the impossible?
Aug 26, 2018
Everlast
Aug 26, 2018
Paul Backus
Aug 28, 2018
bauss
Aug 28, 2018
Everlast
Aug 28, 2018
bauss
Aug 28, 2018
Paul Backus
Aug 28, 2018
Everlast
Aug 28, 2018
Paul Backus
Aug 29, 2018
Everlast
Aug 29, 2018
vit
Aug 29, 2018
Paul Backus
Aug 29, 2018
Everlast
Aug 30, 2018
Paul Backus
Aug 30, 2018
Everlast
Aug 31, 2018
aliak
Aug 30, 2018
Kagamin
Aug 29, 2018
Kagamin
August 26, 2018
void print()
{
}

void print(T, A...)(T t, A a)
{
    import std.stdio;
    writeln(t);
    print(a);
}

The problem is, suppose one wants to specify A

void print(T, int... A)(T t, A a)

while tricks can be used, why doesn't D support such an obvious syntax? We can specify an arbitrary type but can't restrict it in an obvious way, in fact, I'd rather see

void print(T)(T t, int... a)

August 26, 2018
On Sunday, 26 August 2018 at 02:26:58 UTC, Everlast wrote:
> The problem is, suppose one wants to specify A
>
> void print(T, int... A)(T t, A a)
>
> while tricks can be used, why doesn't D support such an obvious syntax? We can specify an arbitrary type but can't restrict it in an obvious way, in fact, I'd rather see
>
> void print(T)(T t, int... a)

https://dlang.org/spec/function.html#typesafe_variadic_functions
August 28, 2018
On Sunday, 26 August 2018 at 02:26:58 UTC, Everlast wrote:
> in fact, I'd rather see
>
> void print(T)(T t, int... a)

You were actually close.

void print(T)(T t, int[] a ...);
August 28, 2018
On Tuesday, 28 August 2018 at 12:00:50 UTC, bauss wrote:
> On Sunday, 26 August 2018 at 02:26:58 UTC, Everlast wrote:
>> in fact, I'd rather see
>>
>> void print(T)(T t, int... a)
>
> You were actually close.
>
> void print(T)(T t, int[] a ...);

Yeah, I see the link paul posted. The actual syntax seems a bit strange to me...

We don't do

A[] a ....

So it is not "logical".

foo(A...)(A a)

but if A is a specific type we must do

foo(int[] a ...)

The actual syntax then looks like we have an variadic set of parameters of type int[] rather than int.


August 28, 2018
On Tuesday, 28 August 2018 at 19:09:38 UTC, Everlast wrote:
> On Tuesday, 28 August 2018 at 12:00:50 UTC, bauss wrote:
>> On Sunday, 26 August 2018 at 02:26:58 UTC, Everlast wrote:
>>> in fact, I'd rather see
>>>
>>> void print(T)(T t, int... a)
>>
>> You were actually close.
>>
>> void print(T)(T t, int[] a ...);
>
> Yeah, I see the link paul posted. The actual syntax seems a bit strange to me...
>
> We don't do
>
> A[] a ....
>
> So it is not "logical".
>
> foo(A...)(A a)
>
> but if A is a specific type we must do
>
> foo(int[] a ...)
>
> The actual syntax then looks like we have an variadic set of parameters of type int[] rather than int.

Yeah I agree with that, but unfortunately it cannot be changed.
August 28, 2018
On Tuesday, 28 August 2018 at 19:09:38 UTC, Everlast wrote:
> Yeah, I see the link paul posted. The actual syntax seems a bit strange to me...
>
> We don't do
>
> A[] a ....
>
> So it is not "logical".
>
> foo(A...)(A a)
>
> but if A is a specific type we must do
>
> foo(int[] a ...)
>
> The actual syntax then looks like we have an variadic set of parameters of type int[] rather than int.

In `foo(A...)(A a)`, a variadic set of template *arguments* is collected into a single template *parameter*, called `A`. In `foo(int[] a ...)`, a variadic set of runtime *arguments* is collected into a single runtime *parameter* called `a`.

(If you've used Python, it's also similar to how `*args` and `**kwargs` collect variadic positional and named arguments into a single list or dictionary parameter, respectively.)

It really is the same underlying principle in both cases. It just looks different because `foo(A...)(A a)` is a template, and `foo(int[] a...)` isn't, and templates use a different syntax than regular functions.

In fact, if you wanted to, you could combine the two and do something like this:

    // All args must have the same type,
    // but it can be any type you want.
    foo(T)(T[] t...)
August 28, 2018
On Tuesday, 28 August 2018 at 19:40:36 UTC, Paul Backus wrote:
> On Tuesday, 28 August 2018 at 19:09:38 UTC, Everlast wrote:
>> Yeah, I see the link paul posted. The actual syntax seems a bit strange to me...
>>
>> We don't do
>>
>> A[] a ....
>>
>> So it is not "logical".
>>
>> foo(A...)(A a)
>>
>> but if A is a specific type we must do
>>
>> foo(int[] a ...)
>>
>> The actual syntax then looks like we have an variadic set of parameters of type int[] rather than int.
>
> In `foo(A...)(A a)`, a variadic set of template *arguments* is collected into a single template *parameter*, called `A`. In `foo(int[] a ...)`, a variadic set of runtime *arguments* is collected into a single runtime *parameter* called `a`.
>
> (If you've used Python, it's also similar to how `*args` and `**kwargs` collect variadic positional and named arguments into a single list or dictionary parameter, respectively.)
>
> It really is the same underlying principle in both cases. It just looks different because `foo(A...)(A a)` is a template, and `foo(int[] a...)` isn't, and templates use a different syntax than regular functions.
>
> In fact, if you wanted to, you could combine the two and do something like this:
>
>     // All args must have the same type,
>     // but it can be any type you want.
>     foo(T)(T[] t...)


Yes, it is the same underlying principle but it is not visually consistent IMO.

e.g.,

foo(int[] a)

a is an array of int

foo(int[] a...)

suggests a is an array of an array of int.

I see ... as a generalization of the parameters simply extending it to an arbitrary number(which cannot be represented except in an arbitrary way).

so

a... means there is an arbitrary number of a's.

int a... means there is an arbitrary number of int.

T a... means there is an arbitrary number of T's

T[] a... means there is an arbitrary number of T[]'s.


For example, to represent an arbitrary number of arrays using the current syntax one must do T[][] a...

which looks funky.

Now, it's all not a big deal in the sense that it all works out by design, but the question is, is it really needed? It seems that requiring [] just adds extra unnecessary symbols that add no useful information.


Also, the biggest complaint is that when we use [] attached to a type it has a specific meaning as "an array of". e.g., int[] means an array of int's.

But int[] a... then changes as we don't have an array of int's any more but simply a sequence of ints. While internally it might not matter it just doesn't jive with normal type syntax IMO.

int[] a...

vs

int a...

the second one seems better. Simpler, more direct, more obvious, and inline with the standard non variadic syntax. The ellipses pretty much state that we are dealing with an array, no reason to add redundancy.

I'm only talking about syntax here and not semantics.

Of course, if you see a problem with int a... not being well-formed then that would be a case against it, but I don't think it has that issue.






August 28, 2018
On Tuesday, 28 August 2018 at 20:37:05 UTC, Everlast wrote:
> Also, the biggest complaint is that when we use [] attached to a type it has a specific meaning as "an array of". e.g., int[] means an array of int's.
>
> But int[] a... then changes as we don't have an array of int's any more but simply a sequence of ints. While internally it might not matter it just doesn't jive with normal type syntax IMO.

The parameter `int[] a...` is an an array of ints just like any other `int[]` in D. If you don't believe me, see for yourself here: https://run.dlang.io/is/IJdovg

I understand that the syntax does not align with your expectations. Instead of complaining about it, I would urge you to set your preconceptions aside and learn D with an open mind. You'll be less frustrated, more productive, and more successful.
August 29, 2018
On Tuesday, 28 August 2018 at 20:37:05 UTC, Everlast wrote:
> the second one seems better. Simpler, more direct, more obvious, and inline with the standard non variadic syntax. The ellipses pretty much state that we are dealing with an array, no reason to add redundancy.

That's not obvious, e.g. C variadic arguments are not an array, and variadic template arguments are not an array either, so a proper type hint is desirable. Also D variadic arguments are not limited to arrays.
August 29, 2018
On Tuesday, 28 August 2018 at 22:01:45 UTC, Paul Backus wrote:
> On Tuesday, 28 August 2018 at 20:37:05 UTC, Everlast wrote:
>> Also, the biggest complaint is that when we use [] attached to a type it has a specific meaning as "an array of". e.g., int[] means an array of int's.
>>
>> But int[] a... then changes as we don't have an array of int's any more but simply a sequence of ints. While internally it might not matter it just doesn't jive with normal type syntax IMO.
>
> The parameter `int[] a...` is an an array of ints just like any other `int[]` in D. If you don't believe me, see for yourself here: https://run.dlang.io/is/IJdovg
>
> I understand that the syntax does not align with your expectations. Instead of complaining about it, I would urge you to set your preconceptions aside and learn D with an open mind. You'll be less frustrated, more productive, and more successful.

One of the things that makes a good language is it's internal syntactic consistency. This makes learning a language easier and also makes remembering it easier. Determinism is a very useful tool as is abstraction consistency. To say "Just except D the way it is" is only because of necessity since that is the way D is, not because it is correct. (There are a lot of incorrect things in the world such as me "learning" D... since I've been programming in D on and off for 10 years, I just never used a specific type for variadics since I've always use a variadic type parameter)

To justify that a poor design choice is necessary is precisely why the poor design choice exists in the first place. These are blemishes on the language not proper design choices.  For example, it is necessary for me to pay taxes, but it does not mean that taxes are necessary.

« First   ‹ Prev
1 2