Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 03, 2017 Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Especially, I mean something like T<S> foo(S,T)(T<S> i) { ... } |
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sighoya | 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)));
}
|
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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
|
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sighoya | 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.
|
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry | 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)))); } |
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sighoya | 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) |
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 |
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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 |
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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)())); } |
October 03, 2017 Re: Does D support nested Templates aka Higher Kinded Polymorphism? | ||||
---|---|---|---|---|
| ||||
Posted in reply to sighoya Attachments:
| 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ž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)())); > } > > > |
Copyright © 1999-2021 by the D Language Foundation