Discussion:
Function parameters from TypeTuple
Tofu Ninja via Digitalmars-d-learn
2014-10-17 17:44:47 UTC
Permalink
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...
ketmar via Digitalmars-d-learn
2014-10-17 17:58:12 UTC
Permalink
On Fri, 17 Oct 2014 17:44:47 +0000
Post by Tofu Ninja via Digitalmars-d-learn
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?
Ali Çehreli via Digitalmars-d-learn
2014-10-17 17:55:14 UTC
Permalink
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
Tofu Ninja via Digitalmars-d-learn
2014-10-17 17:59:11 UTC
Permalink
On Friday, 17 October 2014 at 17:55:14 UTC, Ali Çehreli wrote:
Yeah.. I dont think I was clear the first time...
Tofu Ninja via Digitalmars-d-learn
2014-10-17 17:57:57 UTC
Permalink
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
Tofu Ninja via Digitalmars-d-learn
2014-10-17 18:05:22 UTC
Permalink
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 ...
ketmar via Digitalmars-d-learn
2014-10-17 18:22:03 UTC
Permalink
On Fri, 17 Oct 2014 17:57:57 +0000
Post by Tofu Ninja via Digitalmars-d-learn
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?
Tofu Ninja via Digitalmars-d-learn
2014-10-17 18:35:58 UTC
Permalink
On Friday, 17 October 2014 at 18:22:12 UTC, ketmar via
Post by ketmar via Digitalmars-d-learn
On Fri, 17 Oct 2014 17:57:57 +0000
Tofu Ninja via Digitalmars-d-learn
Post by Tofu Ninja via Digitalmars-d-learn
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.
Ali Çehreli via Digitalmars-d-learn
2014-10-17 18:56:31 UTC
Permalink
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
Justin Whear via Digitalmars-d-learn
2014-10-17 19:51:51 UTC
Permalink
Post by Ali Çehreli via Digitalmars-d-learn
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
anonymous via Digitalmars-d-learn
2014-10-17 19:03:41 UTC
Permalink
Post by Tofu Ninja via Digitalmars-d-learn
Not sure if what I wrote made sense, instead I will just post
the code that is vomiting on me...
You forgot the imports.
Post by Tofu Ninja via Digitalmars-d-learn
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]);
Post by Tofu Ninja via Digitalmars-d-learn
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);
Post by Tofu Ninja via Digitalmars-d-learn
}
}
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]`.
Post by Tofu Ninja via Digitalmars-d-learn
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.
Post by Tofu Ninja via Digitalmars-d-learn
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));
}
Tofu Ninja via Digitalmars-d-learn
2014-10-17 19:18:28 UTC
Permalink
Post by anonymous via Digitalmars-d-learn
Post by Tofu Ninja via Digitalmars-d-learn
Not sure if what I wrote made sense, instead I will just post
the code that is vomiting on me...
You forgot the imports.
Post by Tofu Ninja via Digitalmars-d-learn
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]);
Post by Tofu Ninja via Digitalmars-d-learn
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);
Post by Tofu Ninja via Digitalmars-d-learn
}
}
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]`.
Post by Tofu Ninja via Digitalmars-d-learn
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.
Post by Tofu Ninja via Digitalmars-d-learn
main.d(53): Error: template instance main.multiAccess!() error
instantiating
[...]
These are due to previous errors.
For style points you could
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
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...
anonymous via Digitalmars-d-learn
2014-10-17 19:32:39 UTC
Permalink
Post by Tofu Ninja via Digitalmars-d-learn
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?
Tofu Ninja via Digitalmars-d-learn
2014-10-17 20:25:26 UTC
Permalink
Post by anonymous via Digitalmars-d-learn
Post by Tofu Ninja via Digitalmars-d-learn
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.
Justin Whear via Digitalmars-d-learn
2014-10-17 20:29:34 UTC
Permalink
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

Justin Whear via Digitalmars-d-learn
2014-10-17 18:30:57 UTC
Permalink
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
Loading...