www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct template

reply "deed" <none none.none> writes:
struct Internal { int i; double d; string s; }

struct External_int {
   Internal internal;
    property Internal* ptr () { return &internal; }
	
   this (int a)
   {
     internal.s = "int";
     internal.i = a;
   }
}

struct External (T) {
   Internal internal;
    property Internal* ptr () { return &internal; }
	
   static if (is(typeof(T) == int))
   {
     this (T a)
     {
       internal.s = "int";
       internal.i = a;
     }
   }
}

void main ()
{
   auto e1 = External_int(1); // Ok
   auto e2 = External!int(1); // Nope, cannot implicitly
                              // convert expression (1)
                              // of type int to Internal
}


Why? And how is this fixed? Thanks.
Nov 03 2014
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 3 November 2014 at 17:03:33 UTC, deed wrote:
 struct Internal { int i; double d; string s; }

 struct External_int {
   Internal internal;
    property Internal* ptr () { return &internal; }
 	
   this (int a)
   {
     internal.s = "int";
     internal.i = a;
   }
 }

 struct External (T) {
   Internal internal;
    property Internal* ptr () { return &internal; }
 	
   static if (is(typeof(T) == int))
   {
     this (T a)
     {
       internal.s = "int";
       internal.i = a;
     }
   }
 }

 void main ()
 {
   auto e1 = External_int(1); // Ok
   auto e2 = External!int(1); // Nope, cannot implicitly
                              // convert expression (1)
                              // of type int to Internal
 }


 Why? And how is this fixed? Thanks.
static if (is(typeof(T) == int)) should be static if (is(T == int)) T is already a type.
Nov 03 2014
next sibling parent "deed" <none none.none> writes:
 static if (is(typeof(T) == int))

 should be

 static if (is(T == int))


 T is already a type.
Ahh. Thanks!
Nov 03 2014
prev sibling parent "Meta" <jared771 gmail.com> writes:
On Monday, 3 November 2014 at 17:05:21 UTC, John Colvin wrote:
 static if (is(typeof(T) == int))

 should be

 static if (is(T == int))


 T is already a type.
I thought this was supposed to produce an error message rather than fail silently... I'm positive this used to be an error. Did it change?
Nov 04 2014