Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 31, 2005 Templated Functions | ||||
---|---|---|---|---|
| ||||
In C++ one could do this: template <class T> void genericFunction<T>(someParam1, someParam2, ..., someParamN) {...generic algorithm...} and then: .. genericFunction< list<int> >(blah blah ...); .. Is this possible in D? Tom |
October 31, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomás Rossi | "Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... > In C++ one could do this: > > template <class T> > void genericFunction<T>(someParam1, someParam2, ..., someParamN) > {...generic algorithm...} > > and then: > > .. > genericFunction< list<int> >(blah blah ...); > .. > > Is this possible in D? Yes, it's in the documentation http://www.digitalmars.com/d/template.html template genericFunction (T) { void genericFunction(T p1, T p2, ..., T pN) { } } genericFunction!(list!(int))(...); > > Tom |
October 31, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | In article <dk52us$qpb$1@digitaldaemon.com>, John C says... > >"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... >> In C++ one could do this: >> >> template <class T> >> void genericFunction<T>(someParam1, someParam2, ..., someParamN) >> {...generic algorithm...} >> >> and then: >> >> .. >> genericFunction< list<int> >(blah blah ...); >> .. >> >> Is this possible in D? > >Yes, it's in the documentation http://www.digitalmars.com/d/template.html > >template genericFunction (T) { > void genericFunction(T p1, T p2, ..., T pN) { } >} > >genericFunction!(list!(int))(...); > >> >> Tom > > Ok, I read the docs but the syntax was not so clear to me. Anyway, the C++ syntax (not referring about <>) is a lot better in the sense that you are not bound to name your template: /////////////////////////////////////// template genericFunctions(T) { T genericFunction1() {return T.max;} T genericFunction2() {return T.min;} .. } toString( genericFunctions.genericFunction1!(int)() ); toString( genericFunctions.genericFunction2!(int)() ); .. /////////////////////////////////////// It just looks ugly! What about having some kind of anonymous template. (Please correct me if i'm wrong) Tom |
October 31, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomás Rossi | "Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk5lqe$1gar$1@digitaldaemon.com... > In article <dk52us$qpb$1@digitaldaemon.com>, John C says... >> >>"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... >>> In C++ one could do this: >>> >>> template <class T> >>> void genericFunction<T>(someParam1, someParam2, ..., someParamN) >>> {...generic algorithm...} >>> >>> and then: >>> >>> .. >>> genericFunction< list<int> >(blah blah ...); >>> .. >>> >>> Is this possible in D? >> >>Yes, it's in the documentation http://www.digitalmars.com/d/template.html >> >>template genericFunction (T) { >> void genericFunction(T p1, T p2, ..., T pN) { } >>} >> >>genericFunction!(list!(int))(...); >> >>> >>> Tom >> >> > > Ok, I read the docs but the syntax was not so clear to me. > Anyway, the C++ syntax (not referring about <>) is a lot better in the > sense > that you are not bound to name your template: If you have a single member in the template, you don't have to. > > /////////////////////////////////////// > template genericFunctions(T) > { > T genericFunction1() {return T.max;} > T genericFunction2() {return T.min;} > .. > } > > toString( genericFunctions.genericFunction1!(int)() ); > toString( genericFunctions.genericFunction2!(int)() ); And that could be rewritten as: template genericFunction1(T) { T genericFunction1() { return T.max; } } template genericFunction2(T) { T genericFunction2() { return T.min; } } toString(genericFunction1!(int)); toString(genericFunction2!(int)); > .. > /////////////////////////////////////// > > It just looks ugly! > > What about having some kind of anonymous template. (Please correct me if > i'm > wrong) Do you mean implicit template instantiation? Apparently it'll be in a future version. > > Tom |
October 31, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | In article <dk5min$1h46$1@digitaldaemon.com>, John C says... > >"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk5lqe$1gar$1@digitaldaemon.com... >> In article <dk52us$qpb$1@digitaldaemon.com>, John C says... >>> >>>"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... >>>> In C++ one could do this: >>>> >>>> template <class T> >>>> void genericFunction<T>(someParam1, someParam2, ..., someParamN) >>>> {...generic algorithm...} >>>> >>>> and then: >>>> >>>> .. >>>> genericFunction< list<int> >(blah blah ...); >>>> .. >>>> >>>> Is this possible in D? >>> >>>Yes, it's in the documentation http://www.digitalmars.com/d/template.html >>> >>>template genericFunction (T) { >>> void genericFunction(T p1, T p2, ..., T pN) { } >>>} >>> >>>genericFunction!(list!(int))(...); >>> >>>> >>>> Tom >>> >>> >> >> Ok, I read the docs but the syntax was not so clear to me. >> Anyway, the C++ syntax (not referring about <>) is a lot better in the >> sense >> that you are not bound to name your template: > >If you have a single member in the template, you don't have to. How is that? You don't have to do it when using the function, but you have to name your template the same as your templated function. > >> >> /////////////////////////////////////// >> template genericFunctions(T) >> { >> T genericFunction1() {return T.max;} >> T genericFunction2() {return T.min;} >> .. >> } >> >> toString( genericFunctions.genericFunction1!(int)() ); >> toString( genericFunctions.genericFunction2!(int)() ); > >And that could be rewritten as: > > template genericFunction1(T) { > T genericFunction1() { return T.max; } > } > template genericFunction2(T) { > T genericFunction2() { return T.min; } > } > > toString(genericFunction1!(int)); > toString(genericFunction2!(int)); > >> .. >> /////////////////////////////////////// >> >> It just looks ugly! >> >> What about having some kind of anonymous template. (Please correct me if >> i'm >> wrong) > >Do you mean implicit template instantiation? Apparently it'll be in a future version. > >> >> Tom I was talking about something like this: template (T) { T genericFunction1() {...} T genericFunction2() {...} } Doing: template AAA(T) { T AAA() {...} } template BBB(T) { T BBB() {...} } .. is uglier than C++s: template <class T> T AAA() {...} template <class T> T BBB() {...} .. in the sense that you have to write AAA two times! Tom |
October 31, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomás Rossi | >>If you have a single member in the template, you don't have to. > > How is that? You don't have to do it when using the function, but you have > to > name your template the same as your templated function. It's to do with templates having their own scope, which makes them akin to modules. > > I was talking about something like this: > > template (T) > { > T genericFunction1() {...} > T genericFunction2() {...} > } > > Doing: > > template AAA(T) { T AAA() {...} } > template BBB(T) { T BBB() {...} } > .. > > is uglier than C++s: > > template <class T> T AAA() {...} > template <class T> T BBB() {...} > .. > > in the sense that you have to write AAA two times! Some here have asked for this requirement to be removed for templated functions. > > Tom |
October 31, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomás Rossi | On Mon, 31 Oct 2005 17:55:26 +0000 (UTC), Tomás Rossi wrote: > In article <dk52us$qpb$1@digitaldaemon.com>, John C says... >> >>"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... >>> In C++ one could do this: >>> >>> template <class T> >>> void genericFunction<T>(someParam1, someParam2, ..., someParamN) >>> {...generic algorithm...} >>> >>> and then: >>> >>> .. >>> genericFunction< list<int> >(blah blah ...); >>> .. >>> >>> Is this possible in D? >> >>Yes, it's in the documentation http://www.digitalmars.com/d/template.html >> >>template genericFunction (T) { >> void genericFunction(T p1, T p2, ..., T pN) { } >>} >> >>genericFunction!(list!(int))(...); >> >>> >>> Tom >> >> > > Ok, I read the docs but the syntax was not so clear to me. > Anyway, the C++ syntax (not referring about <>) is a lot better in the sense > that you are not bound to name your template: > > /////////////////////////////////////// > template genericFunctions(T) > { > T genericFunction1() {return T.max;} > T genericFunction2() {return T.min;} > .. > } > > toString( genericFunctions.genericFunction1!(int)() ); > toString( genericFunctions.genericFunction2!(int)() ); > .. > /////////////////////////////////////// > > It just looks ugly! Yes it does. However Walter's suggested solution is to use the 'alias' declaration. template genericFunctions(T) { T genericFunction1() {return T.max;} T genericFunction2() {return T.min;} } // Define some easier names to type and read. alias genericFunctions!(int).genericFunction1 gf1; alias genericFunctions!(int).genericFunction2 gf2; // Show them in use. void main() { gf1(); gf2(); } -- Derek Parnell Melbourne, Australia 1/11/2005 8:30:53 AM |
November 01, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <1vo72jbc04tgw.gwxz3kffp4gh.dlg@40tude.net>, Derek Parnell says... > >On Mon, 31 Oct 2005 17:55:26 +0000 (UTC), Tomás Rossi wrote: > >> In article <dk52us$qpb$1@digitaldaemon.com>, John C says... >>> >>>"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... >>>> In C++ one could do this: >>>> >>>> template <class T> >>>> void genericFunction<T>(someParam1, someParam2, ..., someParamN) >>>> {...generic algorithm...} >>>> >>>> and then: >>>> >>>> .. >>>> genericFunction< list<int> >(blah blah ...); >>>> .. >>>> >>>> Is this possible in D? >>> >>>Yes, it's in the documentation http://www.digitalmars.com/d/template.html >>> >>>template genericFunction (T) { >>> void genericFunction(T p1, T p2, ..., T pN) { } >>>} >>> >>>genericFunction!(list!(int))(...); >>> >>>> >>>> Tom >>> >>> >> >> Ok, I read the docs but the syntax was not so clear to me. >> Anyway, the C++ syntax (not referring about <>) is a lot better in the sense >> that you are not bound to name your template: >> >> /////////////////////////////////////// >> template genericFunctions(T) >> { >> T genericFunction1() {return T.max;} >> T genericFunction2() {return T.min;} >> .. >> } >> >> toString( genericFunctions.genericFunction1!(int)() ); >> toString( genericFunctions.genericFunction2!(int)() ); >> .. >> /////////////////////////////////////// >> >> It just looks ugly! > >Yes it does. However Walter's suggested solution is to use the 'alias' declaration. > >template genericFunctions(T) { > T genericFunction1() {return T.max;} > T genericFunction2() {return T.min;} >} > >// Define some easier names to type and read. >alias genericFunctions!(int).genericFunction1 gf1; >alias genericFunctions!(int).genericFunction2 gf2; > >// Show them in use. >void main() >{ > gf1(); > gf2(); >} The same shit with different color would say my grandfather. :) That solution is a little ugly also and very old fashioned. Since D proclaims to be the cure for mosts of C++ syntax diseases (well, this is what i got at least), i can't see why do i have to appeal to this kind of "C++ style" workarounds. Besides, i don't see (and correct me please in other case) a good reason to dismiss a more elegant solution (builded in the language itself, like other similar problematics were faced), at least in some distant future. Suppose you have this: template GenericFunctions(T) { void doThis() {...} void doThat() {...} T getFoo() {...} void takeFoo(T t) {...} void yourSister() {...} void myCousin() {...} ..etc... } Would you alias one and all? Would you write a template with the func. name for everyone? Would you explicitly use GenericFunctions!(T).func()? Suppose that the language gives you this alternative: template(T) { void doThis() {...} void doThat() {...} T getFoo() {...} void takeFoo(T t) {...} void yourSister() {...} void myCousin() {...} ..etc... } All the above problems are resolved. > >-- >Derek Parnell >Melbourne, Australia >1/11/2005 8:30:53 AM Hope i had maked my point. Tom BsAs, Argentina PS: Sorry for my poor English. |
November 01, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomás Rossi | Tomás Rossi wrote: > In article <1vo72jbc04tgw.gwxz3kffp4gh.dlg@40tude.net>, Derek Parnell says... > >>On Mon, 31 Oct 2005 17:55:26 +0000 (UTC), Tomás Rossi wrote: >> >> >>>In article <dk52us$qpb$1@digitaldaemon.com>, John C says... >>> >>>>"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... >>>> >>>>>In C++ one could do this: >>>>> >>>>>template <class T> >>>>>void genericFunction<T>(someParam1, someParam2, ..., someParamN) >>>>>{...generic algorithm...} >>>>> >>>>>and then: >>>>> >>>>>.. >>>>>genericFunction< list<int> >(blah blah ...); >>>>>.. >>>>> >>>>>Is this possible in D? >>>> >>>>Yes, it's in the documentation http://www.digitalmars.com/d/template.html >>>> >>>>template genericFunction (T) { >>>> void genericFunction(T p1, T p2, ..., T pN) { } >>>>} >>>> >>>>genericFunction!(list!(int))(...); >>>> >>>> >>>>>Tom >>>> >>>> >>>Ok, I read the docs but the syntax was not so clear to me. >>>Anyway, the C++ syntax (not referring about <>) is a lot better in the sense >>>that you are not bound to name your template: >>> >>>/////////////////////////////////////// >>>template genericFunctions(T) { >>>T genericFunction1() {return T.max;} >>>T genericFunction2() {return T.min;} >>>.. >>>} >>> >>>toString( genericFunctions.genericFunction1!(int)() ); >>>toString( genericFunctions.genericFunction2!(int)() ); >>>.. >>>/////////////////////////////////////// >>> >>>It just looks ugly! >> >>Yes it does. However Walter's suggested solution is to use the 'alias' >>declaration. >> >>template genericFunctions(T) { >> T genericFunction1() {return T.max;} >> T genericFunction2() {return T.min;} >>} >> >>// Define some easier names to type and read. >>alias genericFunctions!(int).genericFunction1 gf1; >>alias genericFunctions!(int).genericFunction2 gf2; >> >>// Show them in use. >>void main() >>{ >> gf1(); >> gf2(); >>} > > > The same shit with different color would say my grandfather. :) > That solution is a little ugly also and very old fashioned. Since D proclaims to > be the cure for mosts of C++ syntax diseases (well, this is what i got at > least), i can't see why do i have to appeal to this kind of "C++ style" > workarounds. Besides, i don't see (and correct me please in other case) a good > reason to dismiss a more elegant solution (builded in the language itself, like > other similar problematics were faced), at least in some distant future. > > Suppose you have this: > > template GenericFunctions(T) > { > void doThis() {...} > void doThat() {...} > T getFoo() {...} > void takeFoo(T t) {...} > void yourSister() {...} > void myCousin() {...} > ...etc... > } > > Would you alias one and all? > Would you write a template with the func. name for everyone? Would you explicitly use GenericFunctions!(T).func()? > I think I would probably alias the GenericFunctions!(T) part once and use that. > Suppose that the language gives you this alternative: > > template(T) > { > void doThis() {...} > void doThat() {...} > T getFoo() {...} > void takeFoo(T t) {...} > void yourSister() {...} > void myCousin() {...} > ...etc... > } > > All the above problems are resolved. > And in this case how would you instanciate the template or access any of those functions if the template is anonymous? -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural." |
November 01, 2005 Re: Templated Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruno Medeiros | In article <dk7d63$2pfk$1@digitaldaemon.com>, Bruno Medeiros says... > >Tomás Rossi wrote: >> In article <1vo72jbc04tgw.gwxz3kffp4gh.dlg@40tude.net>, Derek Parnell says... >> >>>On Mon, 31 Oct 2005 17:55:26 +0000 (UTC), Tomás Rossi wrote: >>> >>> >>>>In article <dk52us$qpb$1@digitaldaemon.com>, John C says... >>>> >>>>>"Tomás Rossi" <Tomás_member@pathlink.com> wrote in message news:dk50a0$mmk$1@digitaldaemon.com... >>>>> >>>>>>In C++ one could do this: >>>>>> >>>>>>template <class T> >>>>>>void genericFunction<T>(someParam1, someParam2, ..., someParamN) >>>>>>{...generic algorithm...} >>>>>> >>>>>>and then: >>>>>> >>>>>>.. >>>>>>genericFunction< list<int> >(blah blah ...); >>>>>>.. >>>>>> >>>>>>Is this possible in D? >>>>> >>>>>Yes, it's in the documentation http://www.digitalmars.com/d/template.html >>>>> >>>>>template genericFunction (T) { >>>>> void genericFunction(T p1, T p2, ..., T pN) { } >>>>>} >>>>> >>>>>genericFunction!(list!(int))(...); >>>>> >>>>> >>>>>>Tom >>>>> >>>>> >>>>Ok, I read the docs but the syntax was not so clear to me. >>>>Anyway, the C++ syntax (not referring about <>) is a lot better in the sense >>>>that you are not bound to name your template: >>>> >>>>/////////////////////////////////////// >>>>template genericFunctions(T) >>>>{ >>>>T genericFunction1() {return T.max;} >>>>T genericFunction2() {return T.min;} >>>>.. >>>>} >>>> >>>>toString( genericFunctions.genericFunction1!(int)() ); >>>>toString( genericFunctions.genericFunction2!(int)() ); >>>>.. >>>>/////////////////////////////////////// >>>> >>>>It just looks ugly! >>> >>>Yes it does. However Walter's suggested solution is to use the 'alias' declaration. >>> >>>template genericFunctions(T) { >>> T genericFunction1() {return T.max;} >>> T genericFunction2() {return T.min;} >>>} >>> >>>// Define some easier names to type and read. >>>alias genericFunctions!(int).genericFunction1 gf1; >>>alias genericFunctions!(int).genericFunction2 gf2; >>> >>>// Show them in use. >>>void main() >>>{ >>> gf1(); >>> gf2(); >>>} >> >> >> The same shit with different color would say my grandfather. :) >> That solution is a little ugly also and very old fashioned. Since D proclaims to >> be the cure for mosts of C++ syntax diseases (well, this is what i got at >> least), i can't see why do i have to appeal to this kind of "C++ style" >> workarounds. Besides, i don't see (and correct me please in other case) a good >> reason to dismiss a more elegant solution (builded in the language itself, like >> other similar problematics were faced), at least in some distant future. >> >> Suppose you have this: >> >> template GenericFunctions(T) >> { >> void doThis() {...} >> void doThat() {...} >> T getFoo() {...} >> void takeFoo(T t) {...} >> void yourSister() {...} >> void myCousin() {...} >> ...etc... >> } >> >> Would you alias one and all? >> Would you write a template with the func. name for everyone? >> Would you explicitly use GenericFunctions!(T).func()? >> >I think I would probably alias the GenericFunctions!(T) part once and use that. > >> Suppose that the language gives you this alternative: >> >> template(T) >> { >> void doThis() {...} >> void doThat() {...} >> T getFoo() {...} >> void takeFoo(T t) {...} >> void yourSister() {...} >> void myCousin() {...} >> ...etc... >> } >> >> All the above problems are resolved. >> > >And in this case how would you instanciate the template or access any of those functions if the template is anonymous? For example: takeFoo!(int)(34); char c = getFoo!(char)(); myCousin!(list!(ulong))(); etc. > >-- >Bruno Medeiros - CS/E student >"Certain aspects of D are a pathway to many abilities some consider to >be... unnatural." Tom |
Copyright © 1999-2021 by the D Language Foundation