On Fri, Dec 7, 2012 at 8:59 PM, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisProg@gmx.com" target="_blank">jmdavisProg@gmx.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Friday, December 07, 2012 18:18:30 Gor Gyolchanyan wrote:<br>
> Consider this example:<br>
> this(this)<br>
> {<br>
> _array = _array.dup;<br>
<div class="im"><br>
> Array!int one = Array!int(1, 2, 3, 4, 5);<br>
> immutable Array!int two = one; //Error: conversion error from Array!(int)<br>
> to immutable(Array!(int))<br>
> }<br>
> I enforce value-type semantics by duplicating the arrays on copy, so it<br>
> should behave like a value type with no indirections (implicitly convert to<br>
> immutable).<br>
> What do I need to do for this to work?<br>
<br>
</div>Postblit constructors don't work with const or immutable. If I understand<br>
correctly, Walter and Andrei's solution is to introduce copy constructors, but<br>
while they've discussed their solution in private, they haven't discussed it<br>
in detail in the newsgroup.<br>
<br>
But when you think about how postblit constructors work, there's really no way<br>
to make them work with const or immutable. postblit does a bitwise copy of the<br>
object being copied and then allows you to reassign the member variables other<br>
stuff (e.g. a deep copy). That reassignment is illegal with const or immutable,<br>
and allowing it would violate the type system. You need a way to just do _one_<br>
copy rather than copy and then edit - so basically, you need a copy<br>
constructor.<br>
<br>
Regardless, this is definitely one of the issues with const and immutable that<br>
needs to be fixed for them to be completely viable.<br>
<span class="HOEnZb"><font color="#888888"><br>
- Jonathan M Davis<br>
</font></span></blockquote></div><br>My workaround is to cast away constness. This is safe under my circumstances, because I see to it that it remains unique and doesn't leak mutable references, but it's still a gross hack.<br clear="all">
<div><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br>
</div>