digitalmars.D.learn - struct constructors
- Steven Schveighoffer (41/41) Oct 04 2007 OK,
- div0 (4/30) Oct 04 2007 compiles for me...
- Steven Schveighoffer (6/11) Oct 05 2007 Downloading this now. I think this will fix my issue, I didn't realize ...
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
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 Ycompiles for me... Digital Mars D Compiler v1.021 Copyright (c) 1999-2007 by Digital Mars written by Walter Bright
Oct 04 2007
"div0" wroteDownloading 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. -Steveand the cryptic error (without a file/line number) is: Error: cannot cast int to Ycompiles for me... Digital Mars D Compiler v1.021 Copyright (c) 1999-2007 by Digital Mars written by Walter Bright
Oct 05 2007