Pure functions as initializers for immutable structures?

Michel Fortin michel.fortin at michelf.com
Mon Oct 18 14:44:13 PDT 2010


On 2010-10-18 17:25:49 -0400, Tomek Sowiński <just at ask.me> said:

> 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.

The first one has one interesting property: if at the call site you 
know that all the arguments you're feeding the function with are 
immutable, then you can automatically cast the result to immutable, 
even if the function can also accept const arguments.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list