Fix line length of PatternMatching

pull/135/head
terminalforlife 4 years ago
parent 2ece746c3a
commit 5970584c88

@ -3,39 +3,40 @@
(xs zip ys) map( (x,y) => x*y ) // BAD
// "v42" is interpreted as a name matching any Int value, and "42" is printed.
// BAD
// This is bad.
val v42 = 42
Some(3) match {
case Some(v42) => println("42")
case _ => println("Not 42")
}
// "`v42`" with backticks is interpreted as the existing val v42, and “Not 42” is printed.
// GOOD
// "`v42`" with backticks is interpreted as the existing val v42, and “Not 42”
// is printed. This is good.
val v42 = 42
Some(3) match {
case Some(`v42`) => println("42")
case _ => println("Not 42")
}
// UppercaseVal is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter.
// Thus, the value contained within UppercaseVal is checked against 3, and “Not 42” is printed.
// GOOD
// UppercaseVal is treated as an existing val, rather than a new pattern
// variable, because it starts with an uppercase letter. Thus, the value
// contained within UppercaseVal is checked against 3, and “Not 42” is printed.
// This is good.
val UppercaseVal = 42
Some(3) match {
case Some(UppercaseVal) => println("42")
case _ => println("Not 42")
}
// Creating an alias for a match
// This will maintain the original value passed into the match function, using the '@' symbol, and print "Matched Some(3)"
// Creating an alias for a match. This will maintain the original value passed
// into the match function, using the '@' symbol, and print "Matched Some(3)"
Some(3) match {
case foundSome @ Some(_) => println("Matched " + foundSome)
case _ => println("Matched nothing")
}
// Case Classes
// This method allows you to match on any combination of properties of a case class
// Case Classes. This method allows you to match on any combination of
// properties of a case class
case class Example(a: Int, b: String, c: Boolean)
Example(1, "word", true) match {
case Example(3, _, _) => println("Matches any Example where a = 3")

Loading…
Cancel
Save