struct vs built-in type
Rikki Cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Dec 17 00:06:54 PST 2014
On 17/12/2014 8:57 p.m., Jack Applegame wrote:
> 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
In this case, Bar can be treated as a reference type.
Where as a primitive type such as int is not. It is a literal.
Although I swear remembering this was a compiler bug and there was a
discussion about it.
But just for thought:
struct Foo {
string text;
}
void main() {
Foo foo;
foo.doit;
import std.stdio;
writeln(foo.text);
}
Given definition of doit:
void doit(Foo foo) {
foo.text = "hi";
}
It'll output a new line and thats it.
But for this:
void doit(ref Foo foo) {
foo.text = "hi";
}
It'll output hi.
In this case its explicitly provided by reference not as a literal.
More information about the Digitalmars-d-learn
mailing list