Setting a list of values

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 1 03:49:37 PDT 2016


On Sunday, 1 May 2016 at 05:42:00 UTC, Ali Çehreli wrote:
> On 04/30/2016 10:05 PM, Joel wrote:
> > This has no effect:
> > _bars.each!(a => { a._plots.fillColor = Color(255, 180, 0);
> });
>
> This is a common issue especially for people who know lambdas 
> from other languages. :)
>
> Your lambda does not do any work. Rather, your lambda returns 
> another lambda, which is promptly ignored:
>
> import std.stdio;
> import std.algorithm;
>
> void main() {
>     auto arr = [ 1, 2 ];
>     arr.each!(a => { writeln(a); });  // returns lambda for 
> each a
> }
>
> The lambda that 'each' takes above is "given a, produce this 
> lambda". . To do the intended work, you need to remove the 
> curly braces (and the semicolon):
>
>     arr.each!(a => writeln(a));
>
> Or, you could insert empty () to call the returned lambda but 
> that would completely be extra work in this case:
>
>     arr.each!(a => { writeln(a); }());

Or, remove the arrow:

     arr.each!((a) { writeln(a); });


More information about the Digitalmars-d-learn mailing list