Solving the impossible?
Everlast
Everlast at For.Ever
Tue Aug 28 20:37:05 UTC 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.
More information about the Digitalmars-d-learn
mailing list