www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Function parameters from TypeTuple

reply "Tofu Ninja" <emmons0 purdue.edu> writes:
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...
Oct 17 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Oct 17 2014
parent "Tofu Ninja" <emmons0 purdue.edu> writes:
On Friday, 17 October 2014 at 17:55:14 UTC, Ali Çehreli wrote:
Yeah.. I dont think I was clear the first time...
Oct 17 2014
prev sibling next sibling parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
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
Oct 17 2014
next sibling parent "Tofu Ninja" <emmons0 purdue.edu> writes:
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 ...
Oct 17 2014
prev sibling next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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:
=20
 Not sure if what I wrote made sense, instead I will just post the=20
 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?
Oct 17 2014
parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
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.
Oct 17 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Oct 17 2014
parent Justin Whear <justin economicmodeling.com> writes:
On Fri, 17 Oct 2014 11:56:31 -0700, Ali Çehreli wrote:

 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
http://dlang.org/phobos/std_range.html#transversal
Oct 17 2014
prev sibling parent reply "anonymous" <anonymous example.com> writes:
On Friday, 17 October 2014 at 17:57:58 UTC, Tofu Ninja 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...
You forgot the imports.
 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]);
`Tuple` is a type template. Use `tuple(foo, bar)` to build a `Tuple!(typeof(foo), typeof(bar))`. => return tuple(args[0][i]);
 		else return Tuple!(args[0][i], multiAccess!(Args[1 .. 
 $])(args[1 .. $]));
`Tuple!` -> `tuple` again. Also, `multiAccess` needs `i` again. And you want to `expand` the sub-result into the tuple so that it's flat. => return tuple(args[0][i], multiAccess!(Args[1 .. $])(i, args[1 .. $]).expand);
 	}
 }

 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
[...] This is about you trying to instantiate `Tuple` with the runtime value that is `args[0][1]`.
 main.d(52): Error: tuple index 0 exceeds length 0
[...] I don't know where these come from. They don't show up for me.
 main.d(53): Error: template instance main.multiAccess!() error 
 instantiating
[...] These are due to previous errors. For style points you could * Use the short form for function templates: auto multiAccess(Args ...)(int i, staticMap!(arrayType, Args) args) {...} * Take array types as template arguments instead of constructing them. This allows them to be inferred (IFTI - Implicit Function Template Instantiation): auto multiAccess(Args ...)(int i, Args args) /* Maybe put a template constraint here that forces Args to be all arrays. */ { import std.typecons: tuple; static if(args.length == 1) return tuple(args[0][i]); else return tuple(args[0][i], multiAccess(i, args[1 .. $]).expand); } void main() { int[] a = [1,2]; int[] b = [5,6]; import std.stdio; writeln(multiAccess(1, a,b)); }
Oct 17 2014
parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
On Friday, 17 October 2014 at 19:03:42 UTC, anonymous wrote:
 On Friday, 17 October 2014 at 17:57:58 UTC, Tofu Ninja 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...
You forgot the imports.
 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]);
`Tuple` is a type template. Use `tuple(foo, bar)` to build a `Tuple!(typeof(foo), typeof(bar))`. => return tuple(args[0][i]);
 		else return Tuple!(args[0][i], multiAccess!(Args[1 .. 
 $])(args[1 .. $]));
`Tuple!` -> `tuple` again. Also, `multiAccess` needs `i` again. And you want to `expand` the sub-result into the tuple so that it's flat. => return tuple(args[0][i], multiAccess!(Args[1 .. $])(i, args[1 .. $]).expand);
 	}
 }

 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
[...] This is about you trying to instantiate `Tuple` with the runtime value that is `args[0][1]`.
 main.d(52): Error: tuple index 0 exceeds length 0
[...] I don't know where these come from. They don't show up for me.
 main.d(53): Error: template instance main.multiAccess!() error 
 instantiating
[...] These are due to previous errors. For style points you could * Use the short form for function templates: auto multiAccess(Args ...)(int i, staticMap!(arrayType, Args) args) {...} * Take array types as template arguments instead of constructing them. This allows them to be inferred (IFTI - Implicit Function Template Instantiation): auto multiAccess(Args ...)(int i, Args args) /* Maybe put a template constraint here that forces Args to be all arrays. */ { import std.typecons: tuple; static if(args.length == 1) return tuple(args[0][i]); else return tuple(args[0][i], multiAccess(i, args[1 .. $]).expand); } void main() { int[] a = [1,2]; int[] b = [5,6]; import std.stdio; writeln(multiAccess(1, a,b)); }
I had the imports, I just didn't post them. My problem is most likely that I used Tuple! instead of tuple... which is probably because the differences between the like 20(exaggeration) different types of tuples in D are confusing as hell...
Oct 17 2014
parent reply "anonymous" <anonymous example.com> writes:
On Friday, 17 October 2014 at 19:18:29 UTC, Tofu Ninja wrote:
 I had the imports, I just didn't post them. My problem is most 
 likely that I used Tuple! instead of tuple... which is probably 
 because the differences between the like 20(exaggeration) 
 different types of tuples in D are confusing as hell...
You've seen the rest of my message, right?
Oct 17 2014
parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
On Friday, 17 October 2014 at 19:32:40 UTC, anonymous wrote:
 On Friday, 17 October 2014 at 19:18:29 UTC, Tofu Ninja wrote:
 I had the imports, I just didn't post them. My problem is most 
 likely that I used Tuple! instead of tuple... which is 
 probably because the differences between the like 
 20(exaggeration) different types of tuples in D are confusing 
 as hell...
You've seen the rest of my message, right?
Yeah, the part that fixed it was Tuple! to tuple. Thanks for the help. I think this fixes my problem.
Oct 17 2014
parent Justin Whear <justin economicmodeling.com> writes:
On Fri, 17 Oct 2014 20:25:26 +0000, Tofu Ninja wrote:

 Yeah, the part that fixed it was Tuple! to tuple. Thanks for the help. I
 think this fixes my problem.
I think std.range.transversal already provides the functionality you're looking for. http://dlang.org/phobos/std_range.html#transversal
Oct 17 2014
prev sibling next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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=20
 will generate its parameters to be arrays of the types of a type=20
 tuple.
=20
 So for instance the parameters of f!(int, char) would be (int[],=20
 char[])...
=20
 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?
Oct 17 2014
prev sibling parent Justin Whear <justin economicmodeling.com> writes:
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
Oct 17 2014