Implicit conversion of unique objects to mutable and immutable
Ali Çehreli
acehreli at yahoo.com
Tue Jun 21 17:02:55 PDT 2011
On Tue, 21 Jun 2011 19:04:11 -0400, bearophile wrote:
> Ali Çehreli:
>
>> char[] result = foo();
>> string immutable_result = assumeUnique(result);
>>
>> But that's the wrong thing to do, as foo() may be changed in the future
>> to return a non-unique result.
>
> I think assumeUnique is meant to be used inside foo(), at its end.
You're right: even the documentation example of assumeUnique does that.
But I think the problem here is at the interface. The type conversions
that foo() applies internally doesn't change the original question of
whether to return mutable or immutable. My point is that foo() should not
care, as the data is already mutable. Why force the caller to make a
copy. But I understand that the language doesn't help here.
I wonder whether a UniqueRef object could be returned, which could allow
a single casting of its data to mutable or immutable at the call site.
Further casts could throw, but that would be a runtime solution. :-/
Wait! :) I just grepped and learned about std.typecons.Unique. Ok, that's
the idea but it is not fully implemented yet or doesn't work... Comments
say "doesn't work yet". :-/
Ali
More information about the Digitalmars-d-learn
mailing list