On Wednesday, 4 December 2019 at 22:43:35 UTC, Bastiaan Veelo
wrote:
There is a difference I guess if g() returns a reference type
and is an inout function. immutable y will only work if the
reference returned is immutable.
But not for values?
Const is a promise to the rest of the code that you will never
mutate it. Immutable is a promise by the rest of the code that
it will never mutate.
But if it isn't marked as "shared" then only the current thread
will modify it, so it is only different if you have a mutable
reference as well that could modify the same object as a const
reference.
So if g() always returns immutable, it’s best to receive it as
such, not const. If it can be either, it must be received as
const.
Is there a way to specify in generic code that you want the best
fit of a const/immutable reference depending on the return type
(but not a mutable one)?
I'm comparing D to C++ and I get the following mapping:
Does that make sense at all? D’s const is transitive, C++’s is
not.
Yes, but it is the same for value types.