Const-correctness and uniqueness. Again.

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 9 15:40:30 PST 2015


On Monday, 9 February 2015 at 10:56:31 UTC, Dicebot wrote:
> 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?

Someone is starting to see where I'm getting at when I'm pushing 
for owned...


More information about the Digitalmars-d mailing list