[Challenge] implementing the ambiguous operator in D
Simen kjaeraas
simen.kjaras at gmail.com
Fri Sep 3 16:40:15 PDT 2010
Simen kjaeraas <simen.kjaras at gmail.com> wrote:
> I believe this will only work with arrays as input. Either that, or I
> need
> a way to make this work:
>
> struct Foo( R ) if ( isForwardRange!R ) {
> bool delegate( ElementType!R ) bar;
> Filter!( bar, R ) range;
> }
>
> Or, well, something like it. I need a static type for a Filter that
> delegates to a struct member, in this case bar.
I would have thought this'd work, but apparently I'm wrong:
module foo;
import std.stdio;
import std.algorithm;
import std.range;
struct Foo( R ) if ( isForwardRange!R ) {
R range;
void delegate( ) _popFront;
bool delegate( ) _empty;
ElementType!R delegate( ) _front;
this( R rng ) {
range = rng;
_popFront = { range.popFront( ); };
_empty = { return range.empty; };
_front = { return range.front; };
}
void attempt( bool delegate( ElementType!R ) dg ) {
auto rng = filter!dg( range );
_popFront = { rng.popFront( ); };
_empty = { return rng.empty; };
_front = { return rng.front; };
}
void popFront( ) {
_popFront( );
}
@property
bool empty( ) {
return _empty( );
}
@property
ElementType!R front( ) {
return _front( );
}
}
Foo!R bar( R )( R rng ) if ( isForwardRange!R ) {
return Foo!R( rng );
}
void main() {
auto b = bar( [1,2,3] );
foreach ( e; b ) {
writeln( e );
}
}
Output:
1
object.Error: Access Violation
--
Simen
More information about the Digitalmars-d
mailing list