this() immutable

Simen Kjaeraas simen.kjaras at gmail.com
Wed Oct 16 12:55:27 PDT 2013


On 2013-10-16, 18:54, Daniel Davidson wrote:

> On Thursday, 13 June 2013 at 12:29:57 UTC, Simen Kjaeraas wrote:
>> On Thu, 13 Jun 2013 14:17:22 +0200, Stephan Schiffels  
>> <stephan_schiffels at mac.com> wrote:
>>
>>> For example, is there a way of instantiating an object normally (i.e.  
>>> mutable), and then later "freeze" it to immutable via a simple cast or  
>>> so?
>>
>> In std.exception there is assumeUnique. It's basically just a cast, but
>> might be good enough for you.
>
> Is there any other recourse here?
>
> Why does making `this(...) immutable` fix things below?
> Shouldn't that immutable designation mean no members of this will be  
> modified? But that is the whole point of an initializer? Why does  
> immutable make sense in this context at all?

Immutable in the case of constructors means that the instance will be
created using only data implicitly castable to immutable. That way, when
construction is finished, it is safe for the type system to mark the
result as immutable.


> My problem is a bit more elaborate and unfortunately to initialize  
> members I need to call standard functions that have not been made pure  
> (but should be).

If you're calling functions that are not marked pure in order to create
immutable data, you will need to cast to immutable afterwards. If you
know this is safe, no problem.

It would benefit us all if you reported these functions or created a pull
request for Phobos, of course.

-- 
   Simen


More information about the Digitalmars-d-learn mailing list