www.digitalmars.com         C & C++   DMDScript  

D - template type inside a function

reply "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
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
next sibling parent reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
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' is

 defined" at either lines 4 or 11 (if function insert is commented out).

 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 08 2002
next sibling parent reply Daniel Yokomiso <Daniel_member pathlink.com> writes:
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,

    It's not possible to use the template type inside a function? The
 following template, when compiled, gives the message "identifier 'T' is

 defined" at either lines 4 or 11 (if function insert is commented out).

 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.


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.
Nov 08 2002
parent reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
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 a

 template libray in D with collections, ranges, etc. but I'm stuck with

 functions that don't refer to the template type. This is a personal study

 syntax and semantics. After this I plan to do port some numerics packages

 (like Linpack and Cassowary) and fork the SmartEiffel compiler to generate

 code instead of C.
 I agree with your opinions about D template syntax, it's very verbose. But

 think right now is too soon to fight or complain about it. I prefer to

 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

 C++, but is a PITA to use them.", or something like that ;-). Today we

 tomorrow we rant :-)

 Best regards,
 Daniel Yokomiso.

Nov 08 2002
parent reply "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
"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...
 Hi,

 Your swap doesn't work either. Same error. I'm trying to write a

 template libray in D with collections, ranges, etc. but I'm stuck with

 functions that don't refer to the template type. This is a personal


 of D
 syntax and semantics. After this I plan to do port some numerics


 to D
 (like Linpack and Cassowary) and fork the SmartEiffel compiler to


 D
 code instead of C.
 I agree with your opinions about D template syntax, it's very verbose.


 I
 think right now is too soon to fight or complain about it. I prefer to

 lots of template code and when programmers come to D from other


 they'll say "We have all this nice templates to use in D, faster and


 than
 C++, but is a PITA to use them.", or something like that ;-). Today we

 tomorrow we rant :-)

 Best regards,
 Daniel Yokomiso.


coding.
Nov 08 2002
parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
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
prev sibling next sibling parent reply "Walter" <walter digitalmars.com> writes:
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 on

 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

 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

 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' is

 defined" at either lines 4 or 11 (if function insert is commented out).

 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 18 2002
parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
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 on

 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
 }


Nov 18 2002
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"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 implicit

 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

 little.  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
parent reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
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...
 I still sorta dislike the D template syntax.  I like implicit

 and dislike having the template itself having a name.  Just adds an


 scope to access through.  I suppose aliases or the with statement can


 a
 little.  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
parent reply "Walter" <walter digitalmars.com> writes:
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
next sibling parent reply Carlos <Carlos_member pathlink.com> writes:
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...
 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;
 }


------------------ Carlos Santander http://carlos3.netfirms.com
Nov 19 2002
parent reply "Walter" <walter digitalmars.com> writes:
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...
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



 accepted?

 template Foo(T)
 {
     class B
     {
         T data;
     }
 }

 void Test()
 {
     (new instance Foo(int).B)->data += 4;
 }


------------------ Carlos Santander http://carlos3.netfirms.com

Nov 19 2002
parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
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
prev sibling parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
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...
 So does that mean the instance keyword supported anywhere an expression


 accepted?


Nov 19 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
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' is

 defined" at either lines 4 or 11 (if function insert is commented out).

 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 18 2002