Skip to content

Instantly share code, notes, and snippets.

@zakky-dev
Created January 28, 2014 15:44
Show Gist options
  • Save zakky-dev/8670018 to your computer and use it in GitHub Desktop.
Save zakky-dev/8670018 to your computer and use it in GitHub Desktop.
キャストの有り無しで挙動が変わる
import std.stdio;
interface Functor(T) {
Functor!T fmap(Functor!T delegate(T) lam);
}
class TestFunctor(T) : Functor!T {
private T _value;
this(T value) {
this._value = value;
}
Functor!T fmap(Functor!T delegate(T) lam) {
return lam(this._value);
}
}
unittest {
auto t = new TestFunctor!int(42).fmap((v) {
"t".writeln;
return new TestFunctor!int(v + 42);
});
t.fmap((v) {
"t2".writeln; // 表示されない
return new TestFunctor!int(v + 42);
});
}
unittest {
auto t = new TestFunctor!int(42).fmap((v) {
"t".writeln;
return cast(Functor!int)new TestFunctor!int(v + 42);
});
t.fmap((v) {
"t2".writeln; // 表示される
return cast(Functor!int)new TestFunctor!int(v + 42);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment