Last active
June 11, 2018 06:21
-
-
Save kmizu/886233dd07b7e078e5e4db5ea8de2ce0 to your computer and use it in GitHub Desktop.
Scala考古学:短絡評価は名前渡しとして実装されているか? ref: https://qiita.com/kmizu/items/0925ab2b642992bb4cb0
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
package scala | |
abstract sealed class Boolean extends AnyVal { | |
def && (p: => Boolean): Boolean = // boolean and | |
if (this) p else false | |
def || (p: => Boolean): Boolean = // boolean or | |
if (this) true else p | |
def & (x: Boolean): Boolean = // boolean strict and | |
if (this) x else false | |
def | (x: Boolean): Boolean = // boolean strict or | |
if (this) true else x | |
def == (x: Boolean): Boolean = // boolean equality | |
if (this) x else x.unary_! | |
def != (x: Boolean): Boolean // boolean inequality | |
if (this) x.unary_! else x | |
def unary_!: Boolean // boolean negation | |
if (this) false else true | |
} |
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
scala> val x = true | |
x: Boolean = true | |
scala> x.&& _ | |
res0: Boolean => Boolean = $$Lambda$1047/1553616699@2bb0e277 | |
scala> x.|| _ | |
res1: Boolean => Boolean = $$Lambda$1048/2129021779@1131aead | |
scala> x.| _ | |
res2: Boolean => Boolean = $$Lambda$1049/1592671657@ace2408 | |
scala> def foo(x: => Int): Int = x | |
foo: (x: => Int)Int | |
scala> foo _ | |
res3: (=> Int) => Int = $$Lambda$1057/1348844972@2e9dcdd3 |
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
Note | |
This method uses 'short-circuit' evaluation and behaves as if it was declared as def &&(x: => Boolean): Boolean. If a evaluates to false, false is returned without evaluating b. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment