www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12573] New: Implicit immutable cast for ref/out argument of

https://issues.dlang.org/show_bug.cgi?id=12573

          Issue ID: 12573
           Summary: Implicit immutable cast for ref/out argument of pure
                    functions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

Currently this code compiles and works correctly, it performs an implicit
immutable cast thanks to purity:

string foo1(in string s) pure nothrow {
    auto s2 = s.dup;
    s2[0] = 'a';
    return s2; // OK.
}
void main() {}



But in some cases I'd like to return the result through an argument (perhaps
because the function return value is used for some other purpose, like a
boolean error message). So perhaps it's possible and good to allow code like
this too:


void foo2(in string s, ref string sOut) pure nothrow {
    auto s2 = s.dup;
    s2[0] = 'a';
    sOut = s2; // Error: cannot implicitly convert
}
void foo3(in string s, out string sOut) pure nothrow {
    auto s2 = s.dup;
    s2[0] = 'a';
    sOut = s2; // Error: cannot implicitly convert
}
void main() {}


In dmd 2.066alpha it gives:

test.d(4,12): Error: cannot implicitly convert expression (s2) of type char[]
to string
test.d(9,12): Error: cannot implicitly convert expression (s2) of type char[]
to string

--
Apr 13 2014