July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Reque | "Sean Reque" <seanthenewt@yahoo.com> wrote in message news:g4tjei$31ci$1@digitalmars.com... > Never mind, I wrote the function wrong. This actually works. Alpha compilers have terrible error messages sometimes! > > import std.stdio; > > /* R is return type > * A is first argument type > * U is TypeTuple of rest of argument types > */ > R delegate(U) Curry(R, A, U...)(R delegate(A, U) dg, A arg) > { > return delegate R(U args) { return dg(arg, args); }; > } > > void main() > { > int plus(int x, int y, int z) > { > return x + y + z; > } > > auto plus_two = Curry(&plus, 2); > writefln("%d", plus_two(6, 8)); // prints 16 > } Of course it works, plus is a delegate :) Now put "static" in front of the declaration of plus and watch it fail. |
July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote: > "Sean Reque" <seanthenewt@yahoo.com> wrote in message news:g4tibc$2v3q$1@digitalmars.com... > > > You know, I think you are write and that a static function would work. I would just instantiate templates for every function I wanted to create a new version of. I don't think I quite understood how it worked at first. And if I really needed a delegate, I could easily wrap the invocation of a specific template instantation inside a delegate call and use that. > > Right. You'll have to do it for every function either way. > > > Unfortunately, neither your compose or my compose is working for me right now :(. > > > > Why isn't my implementation working? > > I don't know :). I wrote in this post what I tried: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=12935 |
July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Reque | Reply to Sean, >> It can't. Delegates and functions currently have different calling >> conventions and the compiler cannot automatically convert one to the >> other. Was this just intuition or did you read that it would >> somewhere? >> > Take this example: > [...] > void main() > { > int plus(int x, int y, int z) > { > return x + y + z; > } > auto plus_two = Curry(&plus, 2); > > Notice how the function Curry accepts a delegate, but a function > pointer is actually passed in. I have personally re-written this > function to take advantage of D2 closures and it worked perfectly > fine. > taking the address of a (non static) nested functions generates a delegate, not a function pointer. |
July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> Reply to Sean,
>
> >> It can't. Delegates and functions currently have different calling conventions and the compiler cannot automatically convert one to the other. Was this just intuition or did you read that it would somewhere?
> >>
> > Take this example:
> >
>
> [...]
>
> > void main()
> > {
> > int plus(int x, int y, int z)
> > {
> > return x + y + z;
> > }
> > auto plus_two = Curry(&plus, 2);
> >
> > Notice how the function Curry accepts a delegate, but a function pointer is actually passed in. I have personally re-written this function to take advantage of D2 closures and it worked perfectly fine.
> >
>
> taking the address of a (non static) nested functions generates a delegate, not a function pointer.
>
>
I see. Any idea why this revised function still doesn't work?
R delegate(T) my_compose(R, IR, IT, T...)(IR function(T) first, R function(IT) second) {
return delegate(T args) { return second(first(args)); };
}
test.d(40): template test.my_compose(R,IR,IT,T...) does not match any function template declaration
test.d(40): template test.my_compose(R,IR,IT,T...) cannot deduce template function from argument types !()(shortC function(short, void*, void**),void function(short rc))
test.d(40): Error: function expected before (), not (my_compose(R,IR,IT,T...))((& SQLAllocHandle),(& SQL)) of type int
|
July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Reque | Reply to Sean, > BCS Wrote: > >> Reply to Sean, >> >>>> It can't. Delegates and functions currently have different calling >>>> conventions and the compiler cannot automatically convert one to >>>> the other. Was this just intuition or did you read that it would >>>> somewhere? >>>> >>> Take this example: >>> >> [...] >> >>> void main() >>> { >>> int plus(int x, int y, int z) >>> { >>> return x + y + z; >>> } >>> auto plus_two = Curry(&plus, 2); >>> Notice how the function Curry accepts a delegate, but a function >>> pointer is actually passed in. I have personally re-written this >>> function to take advantage of D2 closures and it worked perfectly >>> fine. >>> >> taking the address of a (non static) nested functions generates a >> delegate, not a function pointer. >> > I see. Any idea why this revised function still doesn't work? > > R delegate(T) my_compose(R, IR, IT, T...)(IR function(T) first, R > function(IT) second) { > return delegate(T args) { return second(first(args)); }; > } > test.d(40): template test.my_compose(R,IR,IT,T...) does not match any > function template declaration > > test.d(40): template test.my_compose(R,IR,IT,T...) cannot deduce > template function from argument types !()(shortC function(short, > void*, void**),void function(short rc)) > > test.d(40): Error: function expected before (), not > (my_compose(R,IR,IT,T...))((& SQLAllocHandle),(& SQL)) of type int > I don't have time right now to dig out the context but... IFTI might just not be up to it. you might try something like this ReturnTypeOf!(S) delegate(ArgsOf!(F)) my_compose(F, S)(F first, S second) { static assert(IsAFunctionType!(F)); // this might do better as a constraint static assert(IsAFunctionType!(S)); return ReturnTypeOf!(S) delegate(ArgsOf!(F) args) { return second(first(args)); }; } I'm sure you can find or write the used templates. http://www.digitalmars.com/d/2.0/phobos/std_traits.html |
July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Reque | "Sean Reque" <seanthenewt@yahoo.com> wrote in message news:g4tjpg$j5$1@digitalmars.com... > Jarrett Billingsley Wrote: > >> "Sean Reque" <seanthenewt@yahoo.com> wrote in message news:g4tibc$2v3q$1@digitalmars.com... >> >> > You know, I think you are write and that a static function would work. >> > I >> > would just instantiate templates for every function I wanted to create >> > a >> > new version of. I don't think I quite understood how it worked at >> > first. >> > And if I really needed a delegate, I could easily wrap the invocation >> > of a >> > specific template instantation inside a delegate call and use that. >> >> Right. You'll have to do it for every function either way. >> >> > Unfortunately, neither your compose or my compose is working for me >> > right >> > now :(. >> > >> >> Why isn't my implementation working? >> >> > > I don't know :). I wrote in this post what I tried: > > http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=12935 And I replied with corrections :) |
July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote:
> "Sean Reque" <seanthenewt@yahoo.com> wrote in message news:g4tjpg$j5$1@digitalmars.com...
> > Jarrett Billingsley Wrote:
> >
> >> "Sean Reque" <seanthenewt@yahoo.com> wrote in message news:g4tibc$2v3q$1@digitalmars.com...
> >>
> >> > You know, I think you are write and that a static function would work.
> >> > I
> >> > would just instantiate templates for every function I wanted to create
> >> > a
> >> > new version of. I don't think I quite understood how it worked at
> >> > first.
> >> > And if I really needed a delegate, I could easily wrap the invocation
> >> > of a
> >> > specific template instantation inside a delegate call and use that.
> >>
> >> Right. You'll have to do it for every function either way.
> >>
> >> > Unfortunately, neither your compose or my compose is working for me
> >> > right
> >> > now :(.
> >> >
> >>
> >> Why isn't my implementation working?
> >>
> >>
> >
> > I don't know :). I wrote in this post what I tried:
> >
> > http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=12935
>
> And I replied with corrections :)
>
>
I am sorry. I missed your corrections. It does work now. Thanks!
|
July 07, 2008 Re: Problem with templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | Thanks BCS. What you gave me was sufficient to make the function. I could not find and did not take the time to right the function IsFunction, but here is the working function in phobos. ReturnType!(S) delegate(ParameterTypeTuple!(F)) my_compose(F, S)(F first, S second) { //no equivalent of IsAFunctionType in phobos //static assert(IsAFunctionType!(F)); // this might do better as a constraint //static assert(IsAFunctionType!(S)); return delegate ReturnType!(S)(ParameterTypeTuple!(F) args) { return second(first(args)); } ; } It's too bad that the using typesafe variadics didn't work, as that solution seems cleaner, but i am very happy to find at least one way to get it to work. Sean |
Copyright © 1999-2021 by the D Language Foundation