Pure functions as initializers for immutable structures?

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 18 12:38:07 PDT 2010


On Mon, 18 Oct 2010 15:20:39 -0400, Tomek Sowiński <just at ask.me> wrote:

> Dnia 18-10-2010 o 20:53:15 Steven Schveighoffer <schveiguy at yahoo.com>  
> napisał(a):
>
>> On Mon, 18 Oct 2010 14:31:26 -0400, Tomek Sowiński <just at ask.me> wrote:
>>
>>> Initializing immutable structures is a source of constant grief.  
>>> Anything non-trivial requires instancing a mutable structure,  
>>> initializing it, and then either casting to immutable (it's up to you  
>>> to ensure no alias leaked) or, not to violate the type system,  
>>> duplicate the whole.
>>
>> Aren't we supposed to get so-called immutable constructors?
>
> Haven't heard, can you point to the discussion?

I think it's in TDPL, don't have a copy so I'm not sure how to refer you.   
I know the really really old pdf document that talks about the future  
direction of D2 had them.

> Anyway, if these immutable ctors are defined inside the type, then the  
> library writer may not provide a ctor suitable for your case (very  
> possible).

Yes true.  However with your method, it's also very possible they don't  
provide a pure ctor.

I think an immutable ctor has different restrictions.  In light of the new  
version of pure, perhaps your idea could obviate the need for immutable  
constructors.  In other words, a pure constructor just got a *lot* easier  
to write, and almost all ctors should be pure.  If that's the case, then  
many would be 'immutably pure' since they take only value types.

> Plus, if the thing I wrote is correct, why not?

Not saying it's not correct, it sounds correct to me, but I think we need  
to verify we aren't creating too many ways to do the same thing here  
(which may even mean immutable constructors are redundant).

>> If that's not what you mean, can you give an example of such grief?
>
> Build an immutable dictionary that would hold the program's options.

This particular problem should be solvable by changing the type of all  
literals to immutable :)

I think at some point, we need something like you propose -- where given  
the properties of a function, you can implicitly convert its return value  
to immutable.  This makes things much easier to deal with.

-Steve


More information about the Digitalmars-d mailing list