Foreach problem
Bill Baxter
wbaxter at gmail.com
Sat Jan 10 19:34:08 PST 2009
On Sun, Jan 11, 2009 at 12:15 PM, Tim M <a at b.com> wrote:
> On Sun, 11 Jan 2009 16:10:39 +1300, Bill Baxter <wbaxter at gmail.com> wrote:
>
>> On Sun, Jan 11, 2009 at 12:04 PM, Tim M <a at b.com> wrote:
>>>
>>> On Sun, 11 Jan 2009 15:59:26 +1300, Tim M <a at b.com> wrote:
>>>>
>>>> Why does it still work for some objects?
>>>
>>>
>>> This works:
>>>
>>>
>>> module test;
>>>
>>> class A
>>> {
>>> this()
>>> {
>>> //
>>> }
>>> }
>>>
>>> class B
>>> {
>>> this()
>>> {
>>> //
>>> }
>>> int opApply (int delegate (inout B) dg)
>>> {
>>> return 1;
>>> }
>>> }
>>>
>>> void main()
>>> {
>>> A a;
>>> B b;
>>> foreach(a; b)
>>> {
>>> //
>>> }
>>> }
>>
>> Interesting. But there the inner 'a' is actually a B. So it
>> compiles, but there's no way it's using the outer 'a' as the counter
>> variable.
>>
>> --bb
>
> Sorry for my typo but change that line to:
> int opApply (int delegate (inout A) dg)
>
> and it still compiles.
'Still compiles" or "then it will compile"?
Anyway, I'm curious if it's using the outer A or not. Can you add a
line or two to actually modify 'a' in the loop?
--bb
More information about the Digitalmars-d-learn
mailing list