[Issue 5491] filter cannot be used in a function?

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 24 18:54:54 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=5491


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #7 from bearophile_hugs at eml.cc 2012-04-24 18:55:55 PDT ---
This bug report, and the code examples, are rather messy.

This code can't work because Phobos filter() returns a range, so if you want a
UsbDevice[] you need to attach an ".array()" at the end.

UsbDevice [] gateways_only( UsbDevice []devices ){ return filter!("a.dev_type
== UsbDevType.gateway" )( devices ); }


And because of the way "string lambdas" are defined, they can't work well here.
But we have the lambda syntax now.

So code like this works:


import std.algorithm, std.conv, std.stdio, std.array;

enum UsbDevType: uint { indifferent = 0, parent_of_checked = 1, hub_dub_7 =
0x2001f103, spider_hub = 0x05e30608, gateway = 0x11A03232 }
struct UsbDevice { //{{{
    UsbDevType dev_type;
    // not essential here ...
    string toString(){ return to!string( dev_type ); }
}

UsbDevice [] gateways_only( UsbDevice []devices ){ return filter!(a =>
a.dev_type
== UsbDevType.gateway)( devices ).array(); }

void main() {
    auto devices  = [ UsbDevice( UsbDevType.gateway ), UsbDevice(
UsbDevType.indifferent ), UsbDevice( UsbDevType.hub_dub_7 ) ];
    auto gateways = gateways_only( devices );
    foreach( UsbDevice dev; gateways ) writeln( dev.toString() );
}


So there is no bug in DMD/Phobos here, closed as INVALID.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list