pure or not pure?
Georg Wrede
georg at nospam.org
Thu Apr 10 12:17:29 PDT 2008
Janice Caron wrote:
> On 10/04/2008, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
>
>> char[] c = new char[5];
>>
>> is invalid inside a pure function because the compiler cannot verify
>> uniqueness of mutable heap data, right?
>
> No. As I said, new is special. It's integral to the language.
First of all, I think new is special anyhow.
Second, come to think of it, even if new weren't special, whatever it
returns IS UNIQUE and as good as IMMUTABLE by others -- as seen FROM THE
PERSPECTIVE OF THE CALLER.
Which is actually all we care about, when we're inside a pure function.
In other words, if you new something, nobody else gets to change it
since nobody else has a reference to this new data.
---
Ehrm. On second thought, it actually IS possible to end up in a
situation where a just newed object may suddenly change in your hands,
and also be referenced from other parts of the program.
(I'll leave constructing an example as an exercise.)
Now, the simplest thing to guarantee that not happening would be to deny
newing of objects that have constructors.
When the compiler gets more advanced, it could examine such
constructors, and only warn (or deny) newing objects where it can't
verify such is not the case.
Until that, I guess we'll see what A/W have decided on this one. If,
that is, they've come up with the above problem.
More information about the Digitalmars-d
mailing list