Should conversion of mutable return value to immutable allowed?

Tomek Sowiński just at ask.me
Thu Feb 24 12:25:45 PST 2011


Ali Çehreli napisał:

> Implicit conversions to immutable in the following two functions feel 
> harmless. Has this been discussed before?
> 
> string foo()
> {
>      char[] s;
>      return s;     // Error: cannot implicitly convert expression
>                    //        (s) of type char[] to string
> }
> 
> string bar()
> {
>      char[] s;
>      return s ~ s; // Error: cannot implicitly convert expression
>                    //        (s ~ s) of type char[] to string
> }
> 
> Is there a reason why that's not possible? I am sure there must be other 
> cases that at least I would find harmless. :)

Indeed. The returned object can be safely set to stone when its only aliases to the outside world point to immutable data. Such a guarantee is expressed in today's language by marking the function pure and all its arguments immutable. The conversion is currently not allowed as the above virtue of immutably pure functions was discovered not too long ago.

If you want it, vote up:
http://d.puremagic.com/issues/show_bug.cgi?id=5081

-- 
Tomek



More information about the Digitalmars-d mailing list