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.
const Foo getFoo()
is the same as
Foo getFoo() const
It makes the invisible this
parameter const
. Because const
is transitive in D, that means that when you try and return a member variable from this
, it's going to be const
as well. If foo
were a value type, then it would just copy it, and then returning a mutable Foo
wouldn't be a problem, because it wouldn't affect the original. But Foo
is a class, and therefore is a reference type. So, returning foo
is returning a reference to the exact same object that State
holds. No copy is made. And so it must be const
- otherwise you would be violating the constness of the this
parameter.
And no, casting away const
is not a good solution. As discussed in this question, casting away const
and 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 actually immutable
, because you can get a segfault in that case. You only cast away const
if 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
:
const(Foo) getFoo() const
Now, you can return foo
and use it. If you want a mutable Foo
, then you either have to not have getFoo
be const
, or you have to have getFoo
return a copy of foo
. e.g.
Foo getFoo() const
{
//This would be cleaner if you provided a clone/dup function
//of some kind on Foo.
auto retval = new Foo;
retval.f = foo.f;
return retval;
}
The important thing for you to take away from here is that const
in D is transitive. As Walter Bright puts it, "it's turtles all the way down." Once something is const
, all parts of it are const
, and don't cast away const
to 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 a const
function, you either need to make the return type const
or create a copy of it and return that.
I suspect that what you really want is this:
class State
{
private Foo foo;
// When `this` is const, return const(Foo).
// When `this` is immutable, return immutable(Foo).
// When `this` is mutable, return Foo.
inout(Foo) getFoo() inout
{
return foo;
}
}
Which is equivalent of:
class State
{
private Foo foo;
Foo getFoo()
{
return foo;
}
const(Foo) getFoo() const
{
// `this.foo` is of type const(Foo), as const is transitive.
return foo;
}
immutable(Foo) getFoo() immutable
{
// `this.foo` is of type immutable(Foo), as immutable is transitive.
return foo;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With