D - template type inside a function
- Daniel Yokomiso (25/25) Nov 07 2002 Hi,
- Sean L. Palmer (26/51) Nov 08 2002 That looks like an oversight. Notice that all the template examples on ...
- Daniel Yokomiso (16/72) Nov 08 2002 Hi,
- Sean L. Palmer (28/43) Nov 08 2002 Try using an alias as a workaround for now.
- Daniel Yokomiso (10/55) Nov 08 2002 study
- Sean L. Palmer (5/10) Nov 08 2002 I'm glad it worked. Alot of C++ compiler bugs can be worked around usin...
- Walter (6/62) Nov 18 2002 This code works now in 0.49. -Walter
- Sean L. Palmer (4/27) Nov 18 2002 Awesome. Thanks!
- Walter (7/11) Nov 18 2002 instantiation,
- Sean L. Palmer (18/29) Nov 18 2002 So does that mean the instance keyword supported anywhere an expression ...
- Walter (3/16) Nov 19 2002 Well, your example compiles!
- Carlos (5/23) Nov 19 2002 ------------------
- Walter (4/32) Nov 19 2002 Oh, right. It compiled when I replaced the -> with a .
-
Sean L. Palmer
(6/10)
Nov 19 2002
Doh!
- Sean L. Palmer (5/10) Nov 19 2002 I'll take that as a yes. ;)
- Walter (5/30) Nov 18 2002 This code now works in 0.49. -Walter
Hi, It's not possible to use the template type inside a function? The following template, when compiled, gives the message "identifier 'T' is not defined" at either lines 4 or 11 (if function insert is commented out). It's a feature or a bug? I'm using dmd 0.48. 1> module bug; 2> template Bug(T) { 3> T[] insert(T[] source, int index, T item) { 4> T[] res = new T[source.length + 1]; 5> res[0..index] = source[0..index]; 6> res[index] = item; 7> res[index + 1 .. res.length] = source[index..source.length]; 8> return res; 9> } 10> T first(T[] source) { 11> T first = source[0]; 12> return first; 13> } 14> } 15> int main() { 16> instance Bug(int) bug; 17> return 0; 18> } Best regards, Daniel Yokomiso.
Nov 07 2002
That looks like an oversight. Notice that all the template examples on the D website have the body stubbed out or do not make use of the template parameter. I'd certainly expect this to work: template Swapper(T) { void Swap(inout T a, inout T b) { T temp = a; a = b; b = temp; } } void foo() { instance Swapper(int) IntSwap; int a=1,b=2; IntSwap.Swap(a,b); printf("a=%d,b=%d\n",a,b); // prints 2,1 } I still sorta dislike the D template syntax. I like implicit instantiation, and dislike having the template itself having a name. Just adds an extra scope to access through. I suppose aliases or the with statement can help a little. But it's better than not having generics at all I suppose. Sean "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> wrote in message news:aqerv9$2g10$1 digitaldaemon.com...Hi, It's not possible to use the template type inside a function? The following template, when compiled, gives the message "identifier 'T' isnotdefined" at either lines 4 or 11 (if function insert is commented out).It'sa feature or a bug? I'm using dmd 0.48. 1> module bug; 2> template Bug(T) { 3> T[] insert(T[] source, int index, T item) { 4> T[] res = new T[source.length + 1]; 5> res[0..index] = source[0..index]; 6> res[index] = item; 7> res[index + 1 .. res.length] = source[index..source.length]; 8> return res; 9> } 10> T first(T[] source) { 11> T first = source[0]; 12> return first; 13> } 14> } 15> int main() { 16> instance Bug(int) bug; 17> return 0; 18> } Best regards, Daniel Yokomiso.
Nov 08 2002
In article <aqfsle$g9k$1 digitaldaemon.com>, Sean L. Palmer says...That looks like an oversight. Notice that all the template examples on the D website have the body stubbed out or do not make use of the template parameter. I'd certainly expect this to work: template Swapper(T) { void Swap(inout T a, inout T b) { T temp = a; a = b; b = temp; } } void foo() { instance Swapper(int) IntSwap; int a=1,b=2; IntSwap.Swap(a,b); printf("a=%d,b=%d\n",a,b); // prints 2,1 } I still sorta dislike the D template syntax. I like implicit instantiation, and dislike having the template itself having a name. Just adds an extra scope to access through. I suppose aliases or the with statement can help a little. But it's better than not having generics at all I suppose. Sean "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> wrote in message news:aqerv9$2g10$1 digitaldaemon.com...Hi, Your swap doesn't work either. Same error. I'm trying to write a comprehensive template libray in D with collections, ranges, etc. but I'm stuck with simple functions that don't refer to the template type. This is a personal study of D syntax and semantics. After this I plan to do port some numerics packages to D (like Linpack and Cassowary) and fork the SmartEiffel compiler to generate D code instead of C. I agree with your opinions about D template syntax, it's very verbose. But I think right now is too soon to fight or complain about it. I prefer to write lots of template code and when programmers come to D from other languages they'll say "We have all this nice templates to use in D, faster and safer than C++, but is a PITA to use them.", or something like that ;-). Today we code tomorrow we rant :-) Best regards, Daniel Yokomiso.Hi, It's not possible to use the template type inside a function? The following template, when compiled, gives the message "identifier 'T' isnotdefined" at either lines 4 or 11 (if function insert is commented out).It'sa feature or a bug? I'm using dmd 0.48. 1> module bug; 2> template Bug(T) { 3> T[] insert(T[] source, int index, T item) { 4> T[] res = new T[source.length + 1]; 5> res[0..index] = source[0..index]; 6> res[index] = item; 7> res[index + 1 .. res.length] = source[index..source.length]; 8> return res; 9> } 10> T first(T[] source) { 11> T first = source[0]; 12> return first; 13> } 14> } 15> int main() { 16> instance Bug(int) bug; 17> return 0; 18> } Best regards, Daniel Yokomiso.
Nov 08 2002
Try using an alias as a workaround for now. template Swapper(T) { alias T T2; void Swap(inout T a, inout T b) { T2 temp = a; a = b; b = temp; } } void foo() { instance Swapper(int) IntSwap; int a=1,b=2; IntSwap.Swap(a,b); printf("a=%d,b=%d\n",a,b); // prints 2,1 } Sean "Daniel Yokomiso" <Daniel_member pathlink.com> wrote in message news:aqga53$uss$1 digitaldaemon.com...Hi, Your swap doesn't work either. Same error. I'm trying to write acomprehensivetemplate libray in D with collections, ranges, etc. but I'm stuck withsimplefunctions that don't refer to the template type. This is a personal studyof Dsyntax and semantics. After this I plan to do port some numerics packagesto D(like Linpack and Cassowary) and fork the SmartEiffel compiler to generateDcode instead of C. I agree with your opinions about D template syntax, it's very verbose. ButIthink right now is too soon to fight or complain about it. I prefer towritelots of template code and when programmers come to D from other languages they'll say "We have all this nice templates to use in D, faster and saferthanC++, but is a PITA to use them.", or something like that ;-). Today wecodetomorrow we rant :-) Best regards, Daniel Yokomiso.
Nov 08 2002
"Sean L. Palmer" <seanpalmer directvinternet.com> escreveu na mensagem news:aqgv3k$1mnl$1 digitaldaemon.com...Try using an alias as a workaround for now. template Swapper(T) { alias T T2; void Swap(inout T a, inout T b) { T2 temp = a; a = b; b = temp; } } void foo() { instance Swapper(int) IntSwap; int a=1,b=2; IntSwap.Swap(a,b); printf("a=%d,b=%d\n",a,b); // prints 2,1 } Sean "Daniel Yokomiso" <Daniel_member pathlink.com> wrote in message news:aqga53$uss$1 digitaldaemon.com...studyHi, Your swap doesn't work either. Same error. I'm trying to write acomprehensivetemplate libray in D with collections, ranges, etc. but I'm stuck withsimplefunctions that don't refer to the template type. This is a personalof Dpackagessyntax and semantics. After this I plan to do port some numericsto Dgenerate(like Linpack and Cassowary) and fork the SmartEiffel compiler toDButcode instead of C. I agree with your opinions about D template syntax, it's very verbose.Ilanguagesthink right now is too soon to fight or complain about it. I prefer towritelots of template code and when programmers come to D from othersaferthey'll say "We have all this nice templates to use in D, faster andthanThanks it worked. It's a very good workaround. Now I can continue with my coding.C++, but is a PITA to use them.", or something like that ;-). Today wecodetomorrow we rant :-) Best regards, Daniel Yokomiso.
Nov 08 2002
I'm glad it worked. Alot of C++ compiler bugs can be worked around using intermediate typedefs. Figured it would be worth a try. Sean "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> wrote in message news:aqh969$212t$1 digitaldaemon.com..."Sean L. Palmer" <seanpalmer directvinternet.com> escreveu na mensagem news:aqgv3k$1mnl$1 digitaldaemon.com...Try using an alias as a workaround for now.Thanks it worked. It's a very good workaround. Now I can continue with my coding.
Nov 08 2002
This code works now in 0.49. -Walter "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:aqfsle$g9k$1 digitaldaemon.com...That looks like an oversight. Notice that all the template examples ontheD website have the body stubbed out or do not make use of the template parameter. I'd certainly expect this to work: template Swapper(T) { void Swap(inout T a, inout T b) { T temp = a; a = b; b = temp; } } void foo() { instance Swapper(int) IntSwap; int a=1,b=2; IntSwap.Swap(a,b); printf("a=%d,b=%d\n",a,b); // prints 2,1 } I still sorta dislike the D template syntax. I like implicitinstantiation,and dislike having the template itself having a name. Just adds an extra scope to access through. I suppose aliases or the with statement can helpalittle. But it's better than not having generics at all I suppose. Sean "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> wrote in message news:aqerv9$2g10$1 digitaldaemon.com...Hi, It's not possible to use the template type inside a function? The following template, when compiled, gives the message "identifier 'T' isnotdefined" at either lines 4 or 11 (if function insert is commented out).It'sa feature or a bug? I'm using dmd 0.48. 1> module bug; 2> template Bug(T) { 3> T[] insert(T[] source, int index, T item) { 4> T[] res = new T[source.length + 1]; 5> res[0..index] = source[0..index]; 6> res[index] = item; 7> res[index + 1 .. res.length] = source[index..source.length]; 8> return res; 9> } 10> T first(T[] source) { 11> T first = source[0]; 12> return first; 13> } 14> } 15> int main() { 16> instance Bug(int) bug; 17> return 0; 18> } Best regards, Daniel Yokomiso.
Nov 18 2002
Awesome. Thanks! Sean "Walter" <walter digitalmars.com> wrote in message news:archhp$2d5b$2 digitaldaemon.com...This code works now in 0.49. -Walter "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:aqfsle$g9k$1 digitaldaemon.com...That looks like an oversight. Notice that all the template examples ontheD website have the body stubbed out or do not make use of the template parameter. I'd certainly expect this to work: template Swapper(T) { void Swap(inout T a, inout T b) { T temp = a; a = b; b = temp; } } void foo() { instance Swapper(int) IntSwap; int a=1,b=2; IntSwap.Swap(a,b); printf("a=%d,b=%d\n",a,b); // prints 2,1 }
Nov 18 2002
"Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:aqfsle$g9k$1 digitaldaemon.com...I still sorta dislike the D template syntax. I like implicitinstantiation,and dislike having the template itself having a name. Just adds an extra scope to access through. I suppose aliases or the with statement can helpalittle. But it's better than not having generics at all I suppose.You can also write: instance Swapper(int).Swap(a,b); if you want to avoid IntSwap.
Nov 18 2002
So does that mean the instance keyword supported anywhere an expression is accepted? template Foo(T) { class B { T data; } } void Test() { (new instance Foo(int).B)->data += 4; } Sean "Walter" <walter digitalmars.com> wrote in message news:archhp$2d5b$3 digitaldaemon.com..."Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:aqfsle$g9k$1 digitaldaemon.com...extraI still sorta dislike the D template syntax. I like implicitinstantiation,and dislike having the template itself having a name. Just adds anhelpscope to access through. I suppose aliases or the with statement canalittle. But it's better than not having generics at all I suppose.You can also write: instance Swapper(int).Swap(a,b); if you want to avoid IntSwap.
Nov 18 2002
Well, your example compiles! "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:arcpgg$2nfo$1 digitaldaemon.com...So does that mean the instance keyword supported anywhere an expression is accepted? template Foo(T) { class B { T data; } } void Test() { (new instance Foo(int).B)->data += 4; }
Nov 19 2002
Is it just me, or it shouldn't compile? I mean... the -> operator... In article <are7uj$1e9t$3 digitaldaemon.com>, Walter says...Well, your example compiles! "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:arcpgg$2nfo$1 digitaldaemon.com...------------------ Carlos Santander http://carlos3.netfirms.comSo does that mean the instance keyword supported anywhere an expression is accepted? template Foo(T) { class B { T data; } } void Test() { (new instance Foo(int).B)->data += 4; }
Nov 19 2002
Oh, right. It compiled when I replaced the -> with a . "Carlos" <Carlos_member pathlink.com> wrote in message news:are9uu$1h5b$1 digitaldaemon.com...Is it just me, or it shouldn't compile? I mean... the -> operator... In article <are7uj$1e9t$3 digitaldaemon.com>, Walter says...isWell, your example compiles! "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:arcpgg$2nfo$1 digitaldaemon.com...So does that mean the instance keyword supported anywhere an expression------------------ Carlos Santander http://carlos3.netfirms.comaccepted? template Foo(T) { class B { T data; } } void Test() { (new instance Foo(int).B)->data += 4; }
Nov 19 2002
Doh! <g> That's what I get for coding in the email editor. It was just to supplement the question with an example. Sean "Walter" <walter digitalmars.com> wrote in message news:arenjn$209n$1 digitaldaemon.com...Oh, right. It compiled when I replaced the -> with a . "Carlos" <Carlos_member pathlink.com> wrote in message news:are9uu$1h5b$1 digitaldaemon.com...Is it just me, or it shouldn't compile? I mean... the -> operator...
Nov 19 2002
I'll take that as a yes. ;) Sean "Walter" <walter digitalmars.com> wrote in message news:are7uj$1e9t$3 digitaldaemon.com...Well, your example compiles! "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message news:arcpgg$2nfo$1 digitaldaemon.com...isSo does that mean the instance keyword supported anywhere an expressionaccepted?
Nov 19 2002
This code now works in 0.49. -Walter "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> wrote in message news:aqerv9$2g10$1 digitaldaemon.com...Hi, It's not possible to use the template type inside a function? The following template, when compiled, gives the message "identifier 'T' isnotdefined" at either lines 4 or 11 (if function insert is commented out).It'sa feature or a bug? I'm using dmd 0.48. 1> module bug; 2> template Bug(T) { 3> T[] insert(T[] source, int index, T item) { 4> T[] res = new T[source.length + 1]; 5> res[0..index] = source[0..index]; 6> res[index] = item; 7> res[index + 1 .. res.length] = source[index..source.length]; 8> return res; 9> } 10> T first(T[] source) { 11> T first = source[0]; 12> return first; 13> } 14> } 15> int main() { 16> instance Bug(int) bug; 17> return 0; 18> } Best regards, Daniel Yokomiso.
Nov 18 2002