Last active
August 6, 2018 23:03
-
-
Save ryan-williams/16914966e656385108c75b681f9f3c5d to your computer and use it in GitHub Desktop.
hazards of lower-case first-letters in `case object` names in scala
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
{ | |
sealed trait obj | |
case object one extends obj | |
case object two extends obj | |
def bad(o: obj) = | |
o match { | |
case one ⇒ 1 | |
case two ⇒ 2 | |
} | |
bad(one) // 1 | |
bad(two) // 1 !!!!! | |
def ok(o: obj) = | |
o match { | |
case _: one.type ⇒ 1 | |
case _: two.type ⇒ 2 | |
} | |
ok(one) // 1 | |
ok(two) // 2 | |
} | |
{ | |
sealed trait obj | |
case class one() extends obj | |
case class two() extends obj | |
def ok(o: obj) = | |
o match { | |
case one() ⇒ 1 | |
case two() ⇒ 2 | |
} | |
ok(one) // 1 | |
ok(two) // 2 | |
} | |
{ | |
sealed trait obj | |
case object One extends obj | |
case object Two extends obj | |
def ok(o: obj) = | |
o match { | |
case One ⇒ 1 | |
case Two ⇒ 2 | |
} | |
ok(one) // 1 | |
ok(two) // 2 | |
} | |
{ | |
sealed trait obj | |
case object `one` extends obj | |
case object `two` extends obj | |
def ok(o: obj) = | |
o match { | |
case `one` ⇒ 1 | |
case `two` ⇒ 2 | |
} | |
ok(one) // 1 | |
ok(two) // 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment