I’ve condensed this problem to a small representative sample:
import std.stdio;
class Foo
{
private int f;
}
class State
{
private Foo foo;
const Foo getFoo()
{
return foo; // This line here.
}
}
void main()
{
auto s = new State;
writeln(s.getFoo());
}
I put that code in test.d.
$ gdmd test.d
test.d:13: Error: cannot implicitly convert expression (this.foo) of type const(Foo) to test.Foo
I understand that it’s telling me to cast the return value with cast(test.Foo)foo, but why? Why does it interpret the member to be of type const(Foo) and why does it need me to cast away that const? I get the feeling that I’m doing something horribly wrong here.
is the same as
It makes the invisible
thisparameterconst. Becauseconstis transitive in D, that means that when you try and return a member variable fromthis, it’s going to beconstas well. Iffoowere a value type, then it would just copy it, and then returning a mutableFoowouldn’t be a problem, because it wouldn’t affect the original. ButFoois a class, and therefore is a reference type. So, returningfoois returning a reference to the exact same object thatStateholds. No copy is made. And so it must beconst– otherwise you would be violating the constness of thethisparameter.And no, casting away
constis not a good solution. As discussed in this question, casting awayconstand then mutating the value is effectively illegal in D. You’re violating the type system when you do that. The compiler will let you do it, but if you’re taking your life into your own hands when you do. It’s especially bad if the underlying object is actuallyimmutable, because you can get a segfault in that case. You only cast awayconstif you absolutely have to, and you never mutate it unless you really know what you’re doing.No, the correct solution is to make the return type
const:Now, you can return
fooand use it. If you want a mutableFoo, then you either have to not havegetFoobeconst, or you have to havegetFooreturn a copy offoo. e.g.The important thing for you to take away from here is that
constin D is transitive. As Walter Bright puts it, “it’s turtles all the way down.” Once something isconst, all parts of it areconst, and don’t cast awayconstto get around it unless you really know what you’re doing. So, if you want to return a reference type which refers to a member variable from aconstfunction, you either need to make the return typeconstor create a copy of it and return that.