Reading about D: few questions

Ali Çehreli acehreli at yahoo.com
Fri Dec 23 14:51:21 PST 2011


On 12/23/2011 11:51 AM, bearophile wrote:

 > Ali:
 >
 >> There is nothing in the language that makes me say "the returned 
object is unique; you can cast it to mutable or immutable freely."<
 >
 > The return value of strongly pure functions is implicitly castable to 
immutable.

Is that working yet? The commented-out lines below don't compile with 2.057:

void main()
{
     char[] s = "hello".dup;

     char[]            am  = endWithDot(s);
     const(char)[]     ac  = endWithDot(s);
     const(char[])     acc = endWithDot(s);
     // immutable(char)[] ai  = endWithDot(s);
     // immutable(char[]) aii = endWithDot(s);
}

pure char[] endWithDot(const(char)[] s)
{
     char[] result = s.dup;
     result ~= '.';
     return result;
}

Also note that I could not use the better line below in endWithDot():

     return s ~ '.';

as the type of the result is const(char)[]. I insist that it too should 
be castable to any mutable or immutable type.

 > And sometimes inout helps.

Yes but it is only when the types of the parameters and the result 
should be related.

 > Bye,
 > bearophile

Ali



More information about the Digitalmars-d-learn mailing list