Requiring weak purity for opAssign, postblit

Jonathan M Davis jmdavisProg at gmx.com
Sat Dec 25 15:12:19 PST 2010


On Saturday 25 December 2010 08:19:32 dsimcha wrote:
> I've been thinking about adding purity to various parts of Phobos and one
> thing that limits this in the case of containers is templated containers,
> arrays, etc. that may be instantiated with a user-defined type.  In theory,
> even assigning/copying a user-defined type could be impure.  Does anyone
> see a problem with requiring opAssign and postblits to be weakly pure to
> solve this problem?

This is one of those things where I want to say that it's a good idea, but I'm 
afraid that it's not. At the moment, I can think of any good reason why it would 
_not_ be a good idea, but I've had plenty of cases in code where I would have 
expected it to be possible to have a function pure and it just didn't work. It 
probably would be quite easy if you had a version of opAssign() that took a type 
other than the type being assigned to, but I'm not sure if we care about being 
able to do that.

What we _really_ need for templates is the ability to make them conditionally 
pure. That is, as long as all of the functions that they call are pure, they're 
pure. This wouldn't work with functions in general, but because templates could 
be checked for purity as they're being instantiated, it would be doable. Without 
that, it's going to be _really_ hard to make a _lot_ of functions pure - 
especially in std.algorithm.

As for this particular case, I _think_ that we could enforce that opAssign() and 
postblit be pure, but if either has to call any helper functions which can't be 
pure, then that's going to be a cause of code duplication in opAssign() and 
postblit constructors. So, it might work, but I'm not 100% sure that it's 
reasonable.

- Jonathan M Davis


More information about the Digitalmars-d mailing list