Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 22, 2020 Passing a variable number of slices into a function | ||||
---|---|---|---|---|
| ||||
Is there any way to pass an unknown number of slices into a function? I'm trying to do something along the lines of: void func(T)(T[] args...) { //... } That wasn't working, so I tried using another generic type where T was already defined: void func(U)(U args...) if(is(U == T[]) { //... } This didn't work either, so I wanted to know if I was going in the right direction or if I was missing something obvious. Thanks for the help! |
June 22, 2020 Re: Passing a variable number of slices into a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to repr-man | On Monday, 22 June 2020 at 01:47:49 UTC, repr-man wrote:
> Is there any way to pass an unknown number of slices into a function? I'm trying to do something along the lines of:
>
> void func(T)(T[] args...)
> {
> //...
> }
>
> That wasn't working,
>
> [...]
>
> Thanks for the help!
Can you provide more details for "that wasnt working" ? because
void func(T)(T[] args...)
{
writeln(args.length);
}
void main()
{
int[] a,b;
func(a[0..$],b[0..$]);
}
works, so there must be a missing detail.
Maybe each slice has different type ?
|
June 22, 2020 Re: Passing a variable number of slices into a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to user1234 | On Monday, 22 June 2020 at 02:04:06 UTC, user1234 wrote:
> Maybe each slice has different type ?
in some cases T[][]... will work better too. depends on the details here....
|
June 22, 2020 Re: Passing a variable number of slices into a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to user1234 | On 6/21/20 10:04 PM, user1234 wrote:
> On Monday, 22 June 2020 at 01:47:49 UTC, repr-man wrote:
>> Is there any way to pass an unknown number of slices into a function? I'm trying to do something along the lines of:
>>
>> void func(T)(T[] args...)
>> {
>> //...
>> }
>>
>> That wasn't working,
>>
>> [...]
>>
>> Thanks for the help!
>
> Can you provide more details for "that wasnt working" ? because
>
> void func(T)(T[] args...)
> {
> writeln(args.length);
> }
>
> void main()
> {
> int[] a,b;
> func(a[0..$],b[0..$]);
> }
>
> works, so there must be a missing detail.
> Maybe each slice has different type ?
I think the problem is that typesafe variadics bind to arrays as well as individual parameters. So func(a[0 .. $]) for instance would resolve as func!int instead of the desired func!(int[]).
The answer might be:
func(T)(T[][] args...)
as Adam says.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation