each & opApply

Steven Schveighoffer schveiguy at yahoo.com
Wed May 23 14:19:31 UTC 2018

On 5/23/18 9:59 AM, Alex wrote:
> On Wednesday, 23 May 2018 at 13:49:45 UTC, Steven Schveighoffer wrote:
>> Right, but not foreach(el1, el2; c), which is the equivalent of your 
>> each call.
> Yes. I tried this in the first place and get a compiler error. But it 
> seemed logical to me, that if I define two opApply overloads, which both 
> matches two arguments, then I need to specify which one I want to use. I 
> achieved this by specifying the types inside the foreach... concisely 
> enough for me :)
> So... I'm looking how to do the same with ´each´, as defining the type 
> of the lambda didn't help.

In your example, you did not define the types for the lambda (you used 
(a, b) => writeln(a, b) ). But I suspect `each` is not going to work 
even if you did. In essence, `each` does not know what the lambda 
requires, especially if it is a typeless lambda. So it essentially needs 
to replicate what foreach would do -- try each of the overloads, and if 
one matches, use it, if none or more than one matches, fail.

I suspect it's more complex, and I'm not sure that it can be done with 
the current tools. But it's definitely a bug that it doesn't work when 
you specify the types.


More information about the Digitalmars-d-learn mailing list