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

           Summary: Modify immutable data using throw
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: [email protected]
        ReportedBy: [email protected]


--- Comment #0 from Tim <[email protected]> 2014-02-09 04:56:54 PST ---
It is possible to throw an immutable Exception and catch it as mutable. Using
this immutable data can be modified.

Example Code:

class Dummy: Exception
{
    int[] data;
    @safe pure nothrow this(immutable int[] data) immutable
    {
        super("Dummy");
        this.data = data;
    }
}
@safe pure void modifyImmutable(immutable int[] data)
{
    try
    {
        immutable Dummy e = new immutable Dummy(data);
        throw e;
    }
    catch(Dummy e)
    {
        e.data[1] = 42;
    }
}
@safe pure void main()
{
    immutable int[] data = [1,2,3];
    assert(data == [1,2,3]);
    modifyImmutable(data);
    assert(data == [1,42,3]);
}

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

Reply via email to