Thread overview
struct constructors
Oct 04, 2007
div0
October 04, 2007
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


October 04, 2007
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
October 05, 2007
"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