struct constructors
Steven Schveighoffer
schveiguy at yahoo.com
Thu Oct 4 10:57:40 PDT 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
More information about the Digitalmars-d-learn
mailing list