Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 17, 2014 Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Basicly what I am trying to do is have a function template that will generate its parameters to be arrays of the types of a type tuple. So for instance the parameters of f!(int, char) would be (int[], char[])... No matter what I try, the compiler vomits all over me... |
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On 10/17/2014 10:44 AM, Tofu Ninja wrote:
> Basicly what I am trying to do is have a function template that will
> generate its parameters to be arrays of the types of a type tuple.
>
> So for instance the parameters of f!(int, char) would be (int[], char[])...
>
> No matter what I try, the compiler vomits all over me...
Perhaps string does not match cha[]? I made the elements const in the following code but you don't need them if you don't need to pass string:
void f(A, B)(const(A)[] as, const(B)[] bs)
{}
void main()
{
f!(int, char)([42], "s");
// And you don't need to specify the template parameters yourself:
f([42], "s");
}
Ali
|
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On Friday, 17 October 2014 at 17:44:48 UTC, Tofu Ninja wrote: Not sure if what I wrote made sense, instead I will just post the code that is vomiting on me... template arrayType(T) { alias arrayType = T[]; } template multiAccess(Args ...) { auto multiAccess(int i, staticMap!(arrayType, Args) args) { static if(args.length == 1) return Tuple!(args[0][i]); else return Tuple!(args[0][i], multiAccess!(Args[1 .. $])(args[1 .. $])); } } void main(string[] args) { int[] a = [1,2]; int[] b = [5,6]; writeln(multiAccess!(int,int)(1, a,b)); } but the compiler really does not like that at all... the error message are very unhelpful as well... Generates 18 errors... main.d(52): Error: variable _param_1 cannot be read at compile time main.d(53): Error: variable _param_1 cannot be read at compile time main.d(52): Error: variable _param_1 cannot be read at compile time main.d(53): Error: variable _param_1 cannot be read at compile time main.d(52): Error: tuple index 0 exceeds length 0 main.d(52): Error: tuple index 0 exceeds 0 main.d(52): Error: tuple index 0 exceeds length 0 main.d(52): Error: tuple index 0 exceeds 0 main.d(52): Error: tuple index 0 exceeds 0 main.d(53): Error: tuple index 0 exceeds length 0 main.d(53): Error: tuple index 0 exceeds 0 main.d(53): Error: tuple index 0 exceeds length 0 main.d(53): Error: tuple index 0 exceeds 0 main.d(53): Error: tuple index 0 exceeds 0 main.d(53): Error: slice [1..0] is out of range of [0..0] main.d(53): Error: template instance main.multiAccess!() error instantiating main.d(53): instantiated from here: multiAccess!int main.d(25): instantiated from here: multiAccess!(int, int) main.d(53): Error: template instance main.multiAccess!int error instantiating main.d(25): instantiated from here: multiAccess!(int, int) main.d(25): Error: template instance main.multiAccess!(int, int) error instantiating |
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja Attachments: | On Fri, 17 Oct 2014 17:44:47 +0000 Tofu Ninja via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Basicly what I am trying to do is have a function template that will generate its parameters to be arrays of the types of a type tuple. > > So for instance the parameters of f!(int, char) would be (int[], char[])... > > No matter what I try, the compiler vomits all over me... i don't really understand what you want, sorry. can you show some more code with use case you want to have? |
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Friday, 17 October 2014 at 17:55:14 UTC, Ali Çehreli wrote: Yeah.. I dont think I was clear the first time... |
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On Friday, 17 October 2014 at 17:57:58 UTC, Tofu Ninja wrote: Also my inability to get this working is probably rooted in my lack of understanding of the differences between tuple vs Tuple vs TypeTuple vs expression tuples ... |
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja Attachments: | On Fri, 17 Oct 2014 17:57:57 +0000 Tofu Ninja via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On Friday, 17 October 2014 at 17:44:48 UTC, Tofu Ninja wrote: > > Not sure if what I wrote made sense, instead I will just post the code that is vomiting on me... still can't grasp what you want to achieve. do you want to build accessor function, or template that returns another template, or what? |
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On Fri, 17 Oct 2014 17:44:47 +0000, Tofu Ninja wrote: > Basicly what I am trying to do is have a function template that will generate its parameters to be arrays of the types of a type tuple. > > So for instance the parameters of f!(int, char) would be (int[], > char[])... > > No matter what I try, the compiler vomits all over me... This what you're thinking of? http://dpaste.dzfl.pl/724bd2573e98 |
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 17 October 2014 at 18:22:12 UTC, ketmar via Digitalmars-d-learn wrote:
> On Fri, 17 Oct 2014 17:57:57 +0000
> Tofu Ninja via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> wrote:
>
>> On Friday, 17 October 2014 at 17:44:48 UTC, Tofu Ninja wrote:
>>
>> Not sure if what I wrote made sense, instead I will just post the code that is vomiting on me...
> still can't grasp what you want to achieve. do you want to build
> accessor function, or template that returns another template, or what?
I am not even sure any more, I am starting to get lost in the tuple madness...
I think I am trying to create an expression tuple for multiple array accesses but I am starting to think that it is not even possible...
First let me ask another question... is it possible to create an expression tuple from an array access? "TypeTyple!(a[1])" clearly does not work even though "a[1]" is an expression. It tries to evaluate the expression "a[1]" instead of creating an expression tuple from it.
|
October 17, 2014 Re: Function parameters from TypeTuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On 10/17/2014 11:35 AM, Tofu Ninja wrote: > I am not even sure any more, I am starting to get lost in the tuple > madness... You want to write a function that takes an index and a number of arrays; and returns an N-ary Tuple where N matches the number arrays passed to the function: :p assert(multiAccess(0, [42], "s") == Tuple!(int, char)(42, 's')); And it should work with any number of parameters. Second elements of three arrays: assert(multiAccess(1, [42, 100], "hello", "world") == Tuple!(int, char, char)(100, 'e', 'o')); Ali |
Copyright © 1999-2021 by the D Language Foundation