Const-correctness and uniqueness. Again.
Dicebot via Digitalmars-d
digitalmars-d at puremagic.com
Mon Feb 9 02:56:30 PST 2015
Doing a lot of porting from D1 to D2 recently I have realized
that we still don't have any idiomatic way to express function
result types that can be both mutable and immutable.
Consider this trivial snippet:
```D
import std.array : join;
void main()
{
auto s = join([ "aaa", "bbb", "ccc" ]);
pragma(msg, typeof(s));
}
```
It outputs "string" which stands for immutable buffer. However,
actual allocated buffer is not yet truly immutable - it has just
been allocated to hold the result of join algorithm and may be
interpreted both as mutable and immutable, depending on caller
desire.
In this specific case if caller wanted to get a mutable buffer
instead, it would need to do cast from immutable which is very
dangerous - especially considering implementation of `join` may
change. And doing .dup on buffer that has just been allocated is
quite a waste.
Right now I am leaning towards personal convention to always
return mutable or const buffers and do assumeUnique at caller
side where necessary. But this does, of course, suck.
What is current conventional wisdom on topic? Can we probably
start using std.typecons.Unique for such functions?
More information about the Digitalmars-d
mailing list