`return const` parameters make `inout` obsolete

Zach the Mystic via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 17 14:58:04 PDT 2015

On Tuesday, 17 March 2015 at 18:27:01 UTC, ixid wrote:
>> To be fully viable, `return` would have to be secretly 
>> recorded as part of the `x's type, so that the compiler could 
>> forgive returning it to a non-const. But the compiler should 
>> probably track that `x` is copied from `t` anyway, so that it 
>> can verify `return t` when it returns `x`, and the same 
>> information would be used to forgive `x's constness.
>> But yeah, there might still be a use for `inout`.
> Why is this ability important? It feels like trying to distort 
> non-templates into templates. Is this the alternative to using 
> templates or repeating yourself or are there other important 
> aspects to it?

I don't know for sure. I think the main point of `inout` is to 
avoid returning a copy of a reference that's mutable and 
assigning it to an immutable. When there is no copy of a 
reference (i.e. it's unique), or if you know that all possible 
copies are immutable, there's no problem. Even an immutable 
reference with lifetime shorter than the `const` value it copies 
is okay.

In other words, it seems like there are a lot of cases where you 
can assign something that returns a regular `T*` to an 
`immutable(T*)` safely.

More information about the Digitalmars-d mailing list