Created
January 28, 2014 15:44
-
-
Save zakky-dev/8670018 to your computer and use it in GitHub Desktop.
キャストの有り無しで挙動が変わる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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