www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct constructors

reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
OK,

So this code doesn't compile:

struct Y
{
  int t;
  static Y opCall(int t)
  {
    Y result;
    result.t = t;
    return result;
  }

  static Y fromLong(long t)
  {
    return Y(cast(int)t);
  }
}

Y v1 = Y(5);
Y v2 = Y.fromLong(5L);


and the cryptic error (without a file/line number) is:
Error: cannot cast int to Y

If I comment out the opCall function, and just use the default builtin 
constructor, like so:

struct Y
{
  int t;
  /*static Y opCall(int t)
  {
    Y result;
    result.t = t;
    return result;
  }*/

  static Y fromLong(long t)
  {
    return Y(cast(int)t);
  }
}

Y v1 = Y(5);
Y v2 = Y.fromLong(5L);

The code compiles just fine.  So what gives?  Is there some rule I don't 
know about?

-Steve 
Oct 04 2007
parent reply div0 <div0 users.sourceforge.net> writes:
Steven Schveighoffer wrote:
 OK,
 
 So this code doesn't compile:
 
 struct Y
 {
   int t;
   static Y opCall(int t)
   {
     Y result;
     result.t = t;
     return result;
   }
 
   static Y fromLong(long t)
   {
     return Y(cast(int)t);
   }
 }
 
 Y v1 = Y(5);
 Y v2 = Y.fromLong(5L);
 
 
 and the cryptic error (without a file/line number) is:
 Error: cannot cast int to Y
compiles for me... Digital Mars D Compiler v1.021 Copyright (c) 1999-2007 by Digital Mars written by Walter Bright
Oct 04 2007
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"div0" wrote
 and the cryptic error (without a file/line number) is:
 Error: cannot cast int to Y
compiles for me... Digital Mars D Compiler v1.021 Copyright (c) 1999-2007 by Digital Mars written by Walter Bright
Downloading this now. I think this will fix my issue, I didn't realize it was a bug, I thought I was doing something wrong :) Found the bug BTW: http://d.puremagic.com/issues/show_bug.cgi?id=1300 Thanks for the help. -Steve
Oct 05 2007