struct vs built-in type
Daniel Kozák via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Dec 17 00:09:32 PST 2014
V Wed, 17 Dec 2014 07:57:24 +0000
Jack Applegame via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> napsáno:
> Code:
>
> import std.stdio;
>
> struct Bar {
> int payload;
> alias payload this;
> }
> struct Foo {
> private {
> Bar m_bar;
> int m_baz;
> }
> @property {
> Bar bar() { return m_bar; }
> void bar(Bar v) { m_bar = v; }
> int baz() { return m_baz; }
> void baz(int v) { m_baz = v; }
> }
> }
>
> void main() {
> Foo foo;
> foo.bar += 4; // Compiles. Why?
> foo.baz += 4; // Error: foo.baz() is not an lvalue
> }
>
> foo.baz() is not lvalue. It's true.
> But foo.bar() is lvalue. Why? I expected the same error. This
> line has no effect, but the compiler does not issue even a
> warning.
>
> DMD64 D Compiler v2.066.1. CentOS 7
This is definitely compiler bug (if you print bar value it is still
zero). This should work only with ref by spec
module main;
import std.stdio;
struct Bar {
int payload;
alias payload this;
}
struct Foo {
private {
Bar m_bar;
int m_baz;
}
@property {
ref Bar bar() { return m_bar; }
void bar(Bar v) { m_bar = v; }
ref int baz() { return m_baz; }
void baz(int v) { m_baz = v; }
}
}
void main() {
Foo foo;
foo.bar += 4;
foo.baz += 4;
writeln(foo.bar);
}
More information about the Digitalmars-d-learn
mailing list