www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does D support nested Templates aka Higher Kinded Polymorphism?

reply sighoya <sighoya gmail.com> writes:
Especially, I mean something like

T<S> foo(S,T)(T<S> i)
{
     ...
}
Oct 03 2017
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 03/10/2017 1:05 PM, sighoya wrote:
 Especially, I mean something like
 
 T<S> foo(S,T)(T<S> i)
 {
      ...
 }
struct Foo(T) { T value; } T!S foo(S, alias T)(T!S v) { return v; } void main() { import std.stdio; writeln(foo!(int, Foo)(Foo!int(1))); }
Oct 03 2017
parent reply sighoya <sighoya gmail.com> writes:
On Tuesday, 3 October 2017 at 12:09:04 UTC, rikki cattermole 
wrote:
 On 03/10/2017 1:05 PM, sighoya wrote:
 Especially, I mean something like
 
 T<S> foo(S,T)(T<S> i)
 {
      ...
 }
struct Foo(T) { T value; } T!S foo(S, alias T)(T!S v) { return v; } void main() { import std.stdio; writeln(foo!(int, Foo)(Foo!int(1))); }
Cool, but it seems that only a nesting of two is allowed, right? This one gives an error: T!S!R bar(alias T,alias S,R)(T!S!R v) {return v;} Error: multiple ! arguments are not allowed
Oct 03 2017
parent reply Jerry <Kickupx gmail.com> writes:
On Tuesday, 3 October 2017 at 12:58:47 UTC, sighoya wrote:
 On Tuesday, 3 October 2017 at 12:09:04 UTC, rikki cattermole 
 wrote:
 On 03/10/2017 1:05 PM, sighoya wrote:
 Especially, I mean something like
 
 T<S> foo(S,T)(T<S> i)
 {
      ...
 }
struct Foo(T) { T value; } T!S foo(S, alias T)(T!S v) { return v; } void main() { import std.stdio; writeln(foo!(int, Foo)(Foo!int(1))); }
Cool, but it seems that only a nesting of two is allowed, right? This one gives an error: T!S!R bar(alias T,alias S,R)(T!S!R v) {return v;} Error: multiple ! arguments are not allowed
That is because you have to wrap multiple with (). Use T!(S!R) instead.
Oct 03 2017
parent reply sighoya <sighoya gmail.com> writes:
Thanks,
How do I construct such a type. I don't get it done right with:

struct Foo(T) {
	T value;	
}

struct Bar (R,S) {
	R!S fooint;
}

T!(S!R) bar(alias T,alias S,R)(T!(S!R) v) {return v;}


void main() {
	import std.stdio;	
	writeln(bar!(Bar,Foo,int)(Bar!(Foo!int(1.0))));
}
Oct 03 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 3 October 2017 at 13:31:00 UTC, sighoya wrote:
 struct Bar (R,S) {
 T!(S!R) bar(alias T,alias S,R)(T!(S!R) v) {return v;}
Bar takes two arguments, but here you are only passing one. Keep in mind that T!(...) is Bar. You called T!(S!(R)) when it was actually written to accept T!(S, R) It might help to forget about the ! and just think of regular functions. Bar(R, S); is the definition but inside you called T(S(R)) when it expected T(S, R)
Oct 03 2017
parent reply sighoya <sighoya gmail.com> writes:
But when I write this to:

writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)));

it complains by:

test.d(11): Error: template instance T!(S!int) does not match 
template declaration Bar(R, S)
test.d(11): Error: template instance T!(S!int) does not match 
template declaration Bar(R, S)
test.d(17): Error: template instance test.bar!(Bar, Foo, int) 
error instantiating
Oct 03 2017
parent reply Daniel Kozak <kozzi11 gmail.com> writes:
writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)()));

Dne 3. 10. 2017 3:55 odpoledne napsal u=C5=BEivatel "sighoya via
Digitalmars-d-learn" <digitalmars-d-learn puremagic.com>:

But when I write this to:

writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)));

it complains by:

test.d(11): Error: template instance T!(S!int) does not match template
declaration Bar(R, S)
test.d(11): Error: template instance T!(S!int) does not match template
declaration Bar(R, S)
test.d(17): Error: template instance test.bar!(Bar, Foo, int) error
instantiating
Oct 03 2017
parent reply sighoya <sighoya gmail.com> writes:
On Tuesday, 3 October 2017 at 15:30:52 UTC, Daniel Kozak wrote:
 writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)()));

 Dne 3. 10. 2017 3:55 odpoledne napsal uživatel "sighoya via 
 Digitalmars-d-learn" <digitalmars-d-learn puremagic.com>:

 But when I write this to:

 writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)));

 it complains by:

 test.d(11): Error: template instance T!(S!int) does not match 
 template
 declaration Bar(R, S)
 test.d(11): Error: template instance T!(S!int) does not match 
 template
 declaration Bar(R, S)
 test.d(17): Error: template instance test.bar!(Bar, Foo, int) 
 error
 instantiating
I still get test.d(11): Error: template instance T!(S!int) does not match template declaration Bar(R, S) test.d(11): Error: template instance T!(S!int) does not match template declaration Bar(R, S) test.d(18): Error: template instance test.bar!(Bar, Foo, int) error instantiating by struct Foo(T) { T value; } struct Bar (R,S) { R!S fooint; } //T!S foo(S, alias T)(T!S v) { return v; } T!(S!R) bar(alias T,alias S,R)(T!(S!R) v) {return v;} void main() { import std.stdio; //writeln(foo!(int, Foo)(Foo!int(1))); //writeln(bar!(Bar,Foo,int)(Bar!(Foo,int))); writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)())); }
Oct 03 2017
next sibling parent Daniel Kozak <kozzi11 gmail.com> writes:
https://run.dlang.io/is/oqbYNb

On Tue, Oct 3, 2017 at 5:52 PM, sighoya via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> wrote:

 On Tuesday, 3 October 2017 at 15:30:52 UTC, Daniel Kozak wrote:

 writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)()));

 Dne 3. 10. 2017 3:55 odpoledne napsal u=C5=BEivatel "sighoya via
 Digitalmars-d-learn" <digitalmars-d-learn puremagic.com>:

 But when I write this to:

 writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)));

 it complains by:

 test.d(11): Error: template instance T!(S!int) does not match template
 declaration Bar(R, S)
 test.d(11): Error: template instance T!(S!int) does not match template
 declaration Bar(R, S)
 test.d(17): Error: template instance test.bar!(Bar, Foo, int) error
 instantiating
I still get test.d(11): Error: template instance T!(S!int) does not match template declaration Bar(R, S) test.d(11): Error: template instance T!(S!int) does not match template declaration Bar(R, S) test.d(18): Error: template instance test.bar!(Bar, Foo, int) error instantiating by struct Foo(T) { T value; } struct Bar (R,S) { R!S fooint; } //T!S foo(S, alias T)(T!S v) { return v; } T!(S!R) bar(alias T,alias S,R)(T!(S!R) v) {return v;} void main() { import std.stdio; //writeln(foo!(int, Foo)(Foo!int(1))); //writeln(bar!(Bar,Foo,int)(Bar!(Foo,int))); writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)())); }
Oct 03 2017
prev sibling parent sighoya <sighoya gmail.com> writes:
On Tuesday, 3 October 2017 at 15:52:52 UTC, sighoya wrote:
 On Tuesday, 3 October 2017 at 15:30:52 UTC, Daniel Kozak wrote:
 writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)()));

 Dne 3. 10. 2017 3:55 odpoledne napsal uživatel "sighoya via 
 Digitalmars-d-learn" <digitalmars-d-learn puremagic.com>:

 But when I write this to:

 writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)));

 it complains by:

 test.d(11): Error: template instance T!(S!int) does not match 
 template
 declaration Bar(R, S)
 test.d(11): Error: template instance T!(S!int) does not match 
 template
 declaration Bar(R, S)
 test.d(17): Error: template instance test.bar!(Bar, Foo, int) 
 error
 instantiating
I still get test.d(11): Error: template instance T!(S!int) does not match template declaration Bar(R, S) test.d(11): Error: template instance T!(S!int) does not match template declaration Bar(R, S) test.d(18): Error: template instance test.bar!(Bar, Foo, int) error instantiating by struct Foo(T) { T value; } struct Bar (R,S) { R!S fooint; } //T!S foo(S, alias T)(T!S v) { return v; } T!(S!R) bar(alias T,alias S,R)(T!(S!R) v) {return v;} void main() { import std.stdio; //writeln(foo!(int, Foo)(Foo!int(1))); //writeln(bar!(Bar,Foo,int)(Bar!(Foo,int))); writeln(bar!(Bar,Foo,int)(Bar!(Foo,int)())); }
Thanks!!!
Oct 03 2017