On 09/02/13 15:49, Joseph Rushton Wakeling wrote: > On 02/09/13 15:39, Artur Skawina wrote: >> While this may not seem ideal, sometimes you do *not* want to do it >> (when you need to keep the original context), so there are no obvious >> alternatives that would handle all cases automagically. > > I'm sure there are valid use cases where this behaviour is desirable. In > fact, all approaches to creating reference-type structs rest on this, > although in those cases the reference is usually a pointer to a payload > rather than a delegate to a function. > > The point is, it's a risk if you don't appreciate how value and reference > types intermingle -- and even when you do in principle, it's easy to > accidentally overlook in practice. The Phobos unittests, which for > RandomSample are very extensive, didn't catch this problem.
Requiring captures to be explicit would reduce the chance of such bugs happening, but also have a significant cost and be a rather drastic change to the language... In this case, there's no need for a delegate, as you do not want to operate on the original object. So you can simply do: //... private void function(ref typeof(this)) _jump; private void jump() { _jump(this); } this(size_t max) { _max = max; _jump = &jump10; } //... static jump10(ref typeof(this)this_) { this_._count += 10; writeln("At end of jump, count = ", this_._count); } It's cheaper than the other alternative (updating ctx in cpctor), but slightly more verbose. More importantly, AFAICT, this is a better fit for the actual problem. artur