-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consider extension methods in Space isSameUnapply
- Loading branch information
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//> using options -Werror | ||
extension (sc: StringContext) | ||
def m: StringContext = sc | ||
def unapply(string: String): Option[String] = | ||
val pattern = sc.parts.head | ||
if string.length == pattern.length then Some(string) else None | ||
|
||
class Test: | ||
def parse(x: PartialFunction[String, String]) = x | ||
|
||
val pf = parse { | ||
case m"x$s" => s | ||
case m"xx$s" => s // was: unreachable | ||
} | ||
|
||
// proof that the second case isn't unreachable (matches "ab") | ||
def t1 = pf.applyOrElse("a", _ => ".") // "a" | ||
def t2 = pf.applyOrElse("ab", _ => ".") // "ab" | ||
def t3 = pf.applyOrElse("abc", _ => ".") // "." |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//> using options -Werror | ||
|
||
// like pos/i18601 | ||
// but with a dedicated SC class | ||
// that made the false positive redundancy warning go away | ||
|
||
extension (sc: StringContext) | ||
def m: SC = SC(sc) | ||
|
||
class SC(sc: StringContext): | ||
def unapply(string: String): Option[String] = | ||
val pattern = sc.parts.head | ||
if string.length == pattern.length then Some(string) else None | ||
|
||
class Test: | ||
def parse(x: PartialFunction[String, String]) = x | ||
|
||
val pf = parse { | ||
case m"x$s" => s | ||
case m"xx$s" => s // was: not unreachable (as a counter-example) | ||
} | ||
|
||
// proof that the second case isn't unreachable (matches "ab") | ||
def t1 = pf.applyOrElse("a", _ => ".") // "a" | ||
def t2 = pf.applyOrElse("ab", _ => ".") // "ab" | ||
def t3 = pf.applyOrElse("abc", _ => ".") // "." |