www.digitalmars.com         C & C++   DMDScript  

D - [Bug] struct template type error

reply Patrick Down <pat codemoon.com> writes:
The following code snippit generated this error:
err.d(9): Foo is used as a type

struct Foo(T)
{
	T x = 0;

	static Foo bar(T c1)
	{
		Foo rtn; // Error here
		rtn.x = c1;
		return rtn;
	}
}

void func()
{
  alias Foo!(double) vector;
  
  vector xAxis = vector.bar(1);
}
Jan 14 2004
parent reply "C" <dont respond.com> writes:
struct Foo(T)
{
 T x = 0;

 static Foo bar(T c1)
 {
  Foo !(double) rtn; // must instantiate with type
  rtn.x = c1;
  return rtn;
 }
}

void func()
{
  alias Foo!(double) vector;
  vector xAxis = vector.bar(1);
}

Howerver if use T to instatiate Foo , I get

identifier 'T' is not defined

Changing the code to
==============

struct Foo(T)
{
 T x = 0;

 static Foo bar(T c1)
 {
  Foo !(typeof(c1) ) rtn; // must instantiate with type
  rtn.x = c1;
  return rtn;
 }
}

void func()
{
  alias Foo!(double) vector;
  vector xAxis = vector.bar(1);
}

void main () { func(); }

Works and is probably what you want ?

C



C

void main () { func(); }
"Patrick Down" <pat codemoon.com> wrote in message
news:Xns9470D8C2F1A87patcodemooncom 63.105.9.61...
 The following code snippit generated this error:
 err.d(9): Foo is used as a type

 struct Foo(T)
 {
 T x = 0;

 static Foo bar(T c1)
 {
 Foo rtn; // Error here
 rtn.x = c1;
 return rtn;
 }
 }

 void func()
 {
   alias Foo!(double) vector;

   vector xAxis = vector.bar(1);
 }

Jan 14 2004
next sibling parent "C" <dont respond.com> writes:
Chaing bar to be a non-static method T is still an undefined identifier ,
why is that ?

C
"C" <dont respond.com> wrote in message
news:bu51u2$boo$1 digitaldaemon.com...
 struct Foo(T)
 {
  T x = 0;

  static Foo bar(T c1)
  {
   Foo !(double) rtn; // must instantiate with type
   rtn.x = c1;
   return rtn;
  }
 }

 void func()
 {
   alias Foo!(double) vector;
   vector xAxis = vector.bar(1);
 }

 Howerver if use T to instatiate Foo , I get

 identifier 'T' is not defined

 Changing the code to
 ==============

 struct Foo(T)
 {
  T x = 0;

  static Foo bar(T c1)
  {
   Foo !(typeof(c1) ) rtn; // must instantiate with type
   rtn.x = c1;
   return rtn;
  }
 }

 void func()
 {
   alias Foo!(double) vector;
   vector xAxis = vector.bar(1);
 }

 void main () { func(); }

 Works and is probably what you want ?

 C



 C

 void main () { func(); }
 "Patrick Down" <pat codemoon.com> wrote in message
 news:Xns9470D8C2F1A87patcodemooncom 63.105.9.61...
 The following code snippit generated this error:
 err.d(9): Foo is used as a type

 struct Foo(T)
 {
 T x = 0;

 static Foo bar(T c1)
 {
 Foo rtn; // Error here
 rtn.x = c1;
 return rtn;
 }
 }

 void func()
 {
   alias Foo!(double) vector;

   vector xAxis = vector.bar(1);
 }


Jan 14 2004
prev sibling next sibling parent Patrick Down <pat codemoon.com> writes:
inline

"C" <dont respond.com> wrote in news:bu51u2$boo$1 digitaldaemon.com:

 struct Foo(T)
 {
  T x = 0;
 
  static Foo bar(T c1)
  {
   Foo !(double) rtn; // must instantiate with type
   rtn.x = c1;
   return rtn;
  }
 }
 
 void func()
 {
   alias Foo!(double) vector;
   vector xAxis = vector.bar(1);
 }
 
 Howerver if use T to instatiate Foo , I get
 
 identifier 'T' is not defined

Yeah I had the same problem. Foo!(T) rtn; should work. I think this is the real bug.
 
 Changing the code to
 ==============
 
 struct Foo(T)
 {
  T x = 0;
 
  static Foo bar(T c1)
  {
   Foo !(typeof(c1) ) rtn; // must instantiate with type
   rtn.x = c1;
   return rtn;
  }
 }
 
 void func()
 {
   alias Foo!(double) vector;
   vector xAxis = vector.bar(1);
 }
 
 void main () { func(); }
 
 Works and is probably what you want ?
 
 C

Yes that works, Thanks.
 
 
 
 C
 
 void main () { func(); }
 "Patrick Down" <pat codemoon.com> wrote in message
 news:Xns9470D8C2F1A87patcodemooncom 63.105.9.61...
 The following code snippit generated this error:
 err.d(9): Foo is used as a type

 struct Foo(T)
 {
 T x = 0;

 static Foo bar(T c1)
 {
 Foo rtn; // Error here
 rtn.x = c1;
 return rtn;
 }
 }

 void func()
 {
   alias Foo!(double) vector;

   vector xAxis = vector.bar(1);
 }


Jan 14 2004
prev sibling parent Patrick Down <pat codemoon.com> writes:
This give me the error:
template instance Foo!(double) Foo is not a template declaration


struct Foo(T)
{
  T x = 0;

  static Foo!(T) bar(T c1)
  {
    Foo!(typeof(c1)) rtn;
    rtn.x = c1;
    return rtn;
  }
}

void func()
{
  alias Foo!(double) vector;
    
  vector xAxis = vector.bar(1);
}

void main () { func(); }


"C" <dont respond.com> wrote in news:bu51u2$boo$1 digitaldaemon.com:

 struct Foo(T)
 {
  T x = 0;
 
  static Foo bar(T c1)
  {
   Foo !(double) rtn; // must instantiate with type
   rtn.x = c1;
   return rtn;
  }
 }
 
 void func()
 {
   alias Foo!(double) vector;
   vector xAxis = vector.bar(1);
 }
 
 Howerver if use T to instatiate Foo , I get
 
 identifier 'T' is not defined
 
 Changing the code to
 ==============
 
 struct Foo(T)
 {
  T x = 0;
 
  static Foo bar(T c1)
  {
   Foo !(typeof(c1) ) rtn; // must instantiate with type
   rtn.x = c1;
   return rtn;
  }
 }
 
 void func()
 {
   alias Foo!(double) vector;
   vector xAxis = vector.bar(1);
 }
 
 void main () { func(); }
 
 Works and is probably what you want ?
 
 C
 
 
 
 C
 
 void main () { func(); }
 "Patrick Down" <pat codemoon.com> wrote in message
 news:Xns9470D8C2F1A87patcodemooncom 63.105.9.61...
 The following code snippit generated this error:
 err.d(9): Foo is used as a type

 struct Foo(T)
 {
 T x = 0;

 static Foo bar(T c1)
 {
 Foo rtn; // Error here
 rtn.x = c1;
 return rtn;
 }
 }

 void func()
 {
   alias Foo!(double) vector;

   vector xAxis = vector.bar(1);
 }


Jan 14 2004