this() immutable

Daniel Davidson nospam at spam.com
Wed Oct 16 13:09:50 PDT 2013


On Wednesday, 16 October 2013 at 19:55:41 UTC, Simen Kjaeraas 
wrote:
> 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.
>

I'm in the learn news group for a reason. I think what you say 
makes sense - a cast is required. But perhaps you have more 
confidence that there is no problem. You and dicebot surely 
disagree on this practice as he sees no real reason to ever 
circumvent the type system.

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

I reported my issue with the `chain` function to this NG and 
tried to start annotating items used by chain with pure to see 
how far the thread led. Honestly it was quickly clear that it led 
too far for me to follow it and someone else indicated the 
problem had to do with Voldermort types. If there is more I could 
do to "benefit us all", beyond learning how it works and what to 
avoid in my own code - I will be glad to try.


More information about the Digitalmars-d-learn mailing list