Skip to content

Instantly share code, notes, and snippets.

@weidagang
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weidagang/5ef3d8a287d5c1cfa475 to your computer and use it in GitHub Desktop.
Save weidagang/5ef3d8a287d5c1cfa475 to your computer and use it in GitHub Desktop.
Make True and False as subtypes of Boolean in Scala (Note that the "object" keyword is the Scala way to declare a singleton class)
abstract class Boolean {
def == (other: Boolean): Boolean;
def != (other: Boolean): Boolean;
def && (other: Boolean): Boolean;
def || (other: Boolean): Boolean;
def unary_! (): Boolean;
}
object True extends Boolean {
def == (other: Boolean) = other;
def != (other: Boolean) = !other;
def && (other: Boolean) = other;
def || (other: Boolean) = True;
def unary_! () = False;
override def toString() = "True";
}
object False extends Boolean {
def == (other: Boolean) = !other;
def != (other: Boolean) = other;
def && (other: Boolean) = False;
def || (other: Boolean) = other;
def unary_! () = True
override def toString = "False";
}
object Test {
def main(args: Array[String]) = {
println("True == True --> " + (True == True));
println("True == False --> " + (True == False));
println("!True --> " + !True);
println("False == !True --> " + (False == !True));
println("!True && (True || False) --> " + (!True && (True || False)));
println("!True || True && False || !False --> " + (!True || True && False || !False));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment