Pitfalls of delegates inside ranges

Artur Skawina art.08.09 at gmail.com
Mon Sep 2 08:44:25 PDT 2013


On 09/02/13 17:06, Joseph Rushton Wakeling wrote:
> On 02/09/13 17:02, Artur Skawina wrote:
>> I'm not sure where the delegates are supposed to be defined, the above
>> allows defining then externally. If that is not required and you only
>> need to select them from a set of predefined internal ones, then you can
>> use your original code with something like:
>>
>>      private void function() _jump;
>>      private void jump() { void delegate() dg; dg.ptr=&this; dg.funcptr=_jump; dg(); }
>>      this(size_t max)
>>      {
>>          _max = max;
>>          _jump = (&jump10).funcptr;
>>      }
>>
>> That way `jump10` (and any other such functions) can still use the implicit-
>> 'this'.
> 
> This second solution may actually rescue my revision to RandomSample -- I'm going to try it out. :-)

The nasty part of that is that the type of .funcptr, hence also of the
'_jump' pointer, is bogus. So calling via '_jump' directly can succeed,
but do the wrong thing. It's a language issue; one reasonable workaround
would be to define a MemberPtr type, which disallows direct calls.

artur


More information about the Digitalmars-d-learn mailing list