digitalmars.D.learn - Static struct assign
- bearophile (15/15) Mar 11 2010 While trying to create a safe int, I have found a problem, this is reduc...
- Ellery Newcomer (2/17) Mar 11 2010 define opCall in Foo
- bearophile (5/6) Mar 11 2010 Thank you :-)
- =?ISO-8859-1?Q?Pelle_M=E5nsson?= (7/22) Mar 11 2010 I think you want a constructor.
While trying to create a safe int, I have found a problem, this is reduced code:
struct Foo {
int x;
static Foo opAssign(int value) { return Foo(value); }
}
void main() {
Foo y = 0;
}
The compiler prints:
test.d(6): Error: cannot implicitly convert expression (0) of type int to Foo
While this generates no error:
Foo y; y = 0; is OK.
Do you know if there is a way to do that?
Bye and thank you,
bearophile
Mar 11 2010
On 03/11/2010 11:22 AM, bearophile wrote:
While trying to create a safe int, I have found a problem, this is reduced
code:
struct Foo {
int x;
static Foo opAssign(int value) { return Foo(value); }
}
void main() {
Foo y = 0;
}
The compiler prints:
test.d(6): Error: cannot implicitly convert expression (0) of type int to Foo
While this generates no error:
Foo y; y = 0; is OK.
Do you know if there is a way to do that?
Bye and thank you,
bearophile
define opCall in Foo
Mar 11 2010
Ellery Newcomer:define opCall in FooThank you :-) I need to practice more with operator overload. Bye, bearophile
Mar 11 2010
On 03/11/2010 06:22 PM, bearophile wrote:
While trying to create a safe int, I have found a problem, this is reduced
code:
struct Foo {
int x;
static Foo opAssign(int value) { return Foo(value); }
}
void main() {
Foo y = 0;
}
The compiler prints:
test.d(6): Error: cannot implicitly convert expression (0) of type int to Foo
While this generates no error:
Foo y; y = 0; is OK.
Do you know if there is a way to do that?
Bye and thank you,
bearophile
I think you want a constructor.
struct Foo {
int x;
static Foo opAssign(int value) { return Foo(value); }
this(int i) {x = i;}
}
Mar 11 2010









bearophile <bearophileHUGS lycos.com> 