On 07/02/2017 08:39 AM, H. S. Teoh via Digitalmars-d wrote:
On Sun, Jul 02, 2017 at 06:49:39AM +0200, Timon Gehr via Digitalmars-d wrote:
On 02.07.2017 06:45, Walter Bright wrote:
On 7/1/2017 9:12 PM, Timon Gehr wrote:
[...]
const(inout(char))[] foo(bool condition, inout(char)[] chars){
      if(!condition) return "condition failed!";
      return chars;
}
[...]

I think the example demonstrate the reason. It either returns the
argument or an immutable global. If the argument is immutable, so is
the return value, otherwise the return value is const.
[...]
In your example, inout makes no sense at all. It should be written as:

        const(char)[] foo(bool condition, const(char)[] chars)

because if it returns the argument, then it's const(char)[], and if it
returns an immutable global, then immutable(char)[] implicitly converts
to const(char)[].

Timon's example makes perfect sense. I don't know if there's an actual need for const(inout), but it enables something you can't do with const:

    string s = foo(true, "bar");

Using inout doesn't make sense here because you
cannot assume that the return value is mutable if the parameter is
mutable -- the function may return immutable instead.

You don't get a mutable result for a mutable argument, but you get an immutable result for an immutable argument. That's what const(inout) enables.

Reply via email to