This file contains 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
String intMatch(int number) { | |
switch (number) { | |
case 1: return "This is number 1."; | |
case 2: return "This is number 2, it comes after 1."; | |
default: return "This number is neither 1 nor 2."; | |
} | |
} |
This file contains 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
def intMatch(number: Int): String = { | |
return number match { | |
case 1 => "This is number 1." | |
case 2 => "This is number 2, it comes after 1." | |
case _ => "This number is neither 1 nor 2." | |
} | |
} |
This file contains 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
def anyMatch(something: Any): String = { | |
return something match { | |
case text: String => text | |
case number: Int => "This is the number " + number + "." | |
case _ => "Unknown thing." | |
} | |
} |
This file contains 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
def variableMatch(expectedNumber: Int, numberGiven: Int): String = { | |
return numberGiven match { | |
case `expectedNumber` => "Wahoo!" | |
case _ => "Doh!" | |
} | |
} |
This file contains 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
def positiveIntMatch(number: Int): String = { | |
return number match { | |
case positive: Int if positive >= 0 => positive + " is greater than or equal to zero." | |
case negative: Int => negative + " is less than zero." | |
} | |
} |
This file contains 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
case class ChocolateCake(val chocolatey: Int) extends Food |
This file contains 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
case class ShotFired(val playerName: String, val atX: Int, val y: Int) | |
val result = new ShotFired("Sean", 2, 5) match { | |
case ShotFired(by, atX, atY) => by + " fired at (" + atX + "," + atY + ")" | |
} |
This file contains 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
case ShotFired(by, atX, atY) => by + " fired at (" + atX + "," + atY + ")" |
This file contains 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
case class MedikitUsed(val name: String, val useAmount: Int) | |
val result = new MedikitUsed("Sean", 0) match { | |
case MedikitUsed(name, 0) => name + " attempted to use none of their medikit and that makes no sense." | |
} |
This file contains 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
abstract class SpecialWeapon | |
case class SpecialWeaponUsed(val player: String, val weapon: SpecialWeapon) | |
case class NuclearBomb(val megatons: Int) extends SpecialWeapon | |
val result = new SpecialWeaponUsed("Sean", new NuclearBomb(17)) match { | |
case SpecialWeaponUsed(name, NuclearBomb(megatons)) => name + " used a " + megatons + " megaton nuclear bomb." | |
} |
OlderNewer