Pitfalls of delegates inside ranges

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Mon Sep 2 07:43:28 PDT 2013


On 02/09/13 16:18, Artur Skawina wrote:
> 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...

For what it's worth, I'm not advocating for any change in the language.  I'm 
simply highlighting a place where it's possible to shoot oneself in the foot :-)

> 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.

Oh, nice thought.  Thank you! :-)


More information about the Digitalmars-d-learn mailing list