[Issue 12282] Immutable result of std.array.array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 9 14:00:12 PDT 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12282


monarchdodra at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra at gmail.com


--- Comment #1 from monarchdodra at gmail.com 2014-03-09 14:00:09 PDT ---
I don't think that can work, at least, not without massive help from the
compiler, and the language itself. Let's forget `array` itself for a moment.

Basically, you want the `const(int)[]` to cast to `immutable(int)[]`. Both you
and I know that this conversion is not legal.

You are probably thinking: Since `array` is pure, then the "uniqueness" of the
result should make the cast possible?

Yes and no... The problem is that `array` *also* takes an array as an argument,
which may reference mutable data, making the function weakly pure. The language
has no way to know if the return value isn't aliasing something in the input
argument. Take this example

```D
void main()
{
    int a[];
    immutable b = array();     //OK
    immutable c = array(null); //OK
    immutable d = array(a);    //OK?
}

const(int[]) array(              ) pure {return (int[]).init;}
const(int[]) array(const(int[]) p) pure {return p;} 
```

Currently, 1) and 2) pass, but 3) fails. For a good reason.

So, unless the language gives us some sort of "noalias" for the return value, I
don't see how your request could be implemented.

The only way I see for it to work would be for `array` to *actually* return an
immutable when possible. But:
1) The code would be difficult to write.
2) It would break auto-inference, since the expected return is "const", and not
"immutable".

I'm not closing, because it is a valid request, but I don't see any way of
making it happen.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list