Pure functions as initializers for immutable structures?

Tomek Sowiński just at ask.me
Mon Oct 18 14:25:49 PDT 2010


Dnia 18-10-2010 o 23:05:27 Don <nospam at nospam.com> napisał(a):

> Michel Fortin wrote:
>> On 2010-10-18 14:31:26 -0400, Tomek Sowiński <just at ask.me> said:
>>
>>> Call me crazy, but I think it is safe to implicitly convert a pure   
>>> function's return value to immutable. What you think?
>>  Well, it depends on the arguments of the pure function. Here's two  
>> cases where it won't work.
> [snip]
>> So I'd precise your assertion by saying it is safe to implicitly  
>> convert a pure function's return value to immutable, but only when all  
>> the arguments you feed to it are immutable.
>
> Tomek stated that at the end of his original post. I think he is correct.
> And no, I don't think it would be difficult to implement.

Thanks for support. I see two ways to go about it:

pure T make(Args args) { ... }
unittest {
     T t = make(...); // good
     immutable T t = make(...); // also good
}

Or:

pure immutable(T) make(Args args) {
     T t = ...;
     // initialize t
     return t; // conversion happens here
}

I like the first one.

-- 
Tomek


More information about the Digitalmars-d mailing list