4

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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135

3 Answers3

5
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.

Community
  • 1
  • 1
Jonathan M Davis
  • 37,181
  • 17
  • 72
  • 102
4

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;
    }
}
jA_cOp
  • 3,275
  • 19
  • 15
3

It interprets the member as const(Foo) because your method signature is const Foo getFoo(), which implies foo will not be mutable.

Remove the const and it should be fine.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • The whole point is to have a const accessor function so other people can see but not modify the data. Is it just assuming that because it's the return statement for a function whose return is const? I would have expected it to want me to cast it *to* const, not *from* const in that case. – nmichaels Apr 28 '12 at 15:36
  • Then you should declare the getter as `Foo getFoo() const`. [A similar question here](http://bytes.com/topic/c/answers/806064-i-could-not-get-const-before-function-declaration) – Alexander Pavlov Apr 28 '12 at 15:39
  • @Alexander: I get the same issue when I do that, and I can't decipher the language spec well enough to tell what it means. – nmichaels Apr 28 '12 at 16:07
  • @nmichaels: a better way to fix it is actually to probably to say const(Foo) instead of Foo, since D interprets the first const as referring to the method. – user541686 Apr 28 '12 at 16:35
  • 3
    @nmichaels const is transient so (cast(const)s).foo will also be const I think the only way is `const(Foo) getFoo() const` – ratchet freak Apr 28 '12 at 17:09