-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix exception on sequence matching with drop #21281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2912c93
Fix exception on sequence matching with drop
KavinSatheeskumar 7e48bd5
Merge branch 'scala:main' into unapplyseq-fix
KavinSatheeskumar c0da66c
Remove commented out code
KavinSatheeskumar ec27f1d
Merge branch 'unapplyseq-fix' of github.com:KavinSatheeskumar/scala3 …
KavinSatheeskumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
object Matcher { | ||
// Chained Match | ||
val chained_match_xs: List[Any] = List(1, 2, 3) | ||
val chained_match_x = chained_match_xs match { | ||
case Nil => "empty" | ||
case _ => "nonempty" | ||
} match { | ||
case "empty" => 0 | ||
case "nonempty" => 1 | ||
} | ||
println(chained_match_x) | ||
|
||
// Vararg Splices | ||
val vararg_arr = Array(0, 1, 2, 3) | ||
val vararg_lst = List(vararg_arr*) // vararg splice argument | ||
// Throws an exception? | ||
val vararg_splice = vararg_lst match | ||
case List(0, 1, xs*) => 1 // binds xs to Seq(2, 3) | ||
case List(1, _*) => 0 // wildcard pattern | ||
case _ => 2 | ||
println(vararg_splice) | ||
println(vararg_lst) | ||
|
||
// Pattern Definitions | ||
val patter_def_xs: List[Any] = List(1, 2, 3) | ||
val (patter_def_x: Any) :: _ = patter_def_xs : @unchecked | ||
println(patter_def_x) | ||
|
||
val patter_def_pair = (1, true) | ||
val (patter_def_a, patter_def_b) = patter_def_pair | ||
println(patter_def_a) | ||
|
||
val elems: List[(Int, Int)] = List((1, 2), (3, 4), (5, 6)) | ||
|
||
for ((x,y) <- elems) do println(x) | ||
|
||
def main(args: Array[String]) = { | ||
// println(chained_match_x) | ||
println(vararg_splice) | ||
// println(patter_def_x) | ||
// println( | ||
} | ||
} | ||
|
||
|
||
// Patter Matching Using Extractors | ||
|
||
// Option Extractors | ||
case class Person(name: String, age: Int) | ||
object Person { | ||
def unapply(person: Person): Option[(String, Int)] = Some((person.name, person.age)) | ||
} | ||
|
||
object OptionMatcher { | ||
val person = Person("Alice", 25) | ||
|
||
val result = person match { | ||
case Person(name, age) => s"Name: $name, Age: $age" | ||
case _ => "Not a person" | ||
} | ||
println(result) | ||
} | ||
|
||
|
||
|
||
// Boolean Extractors | ||
object Adult { | ||
def unapply(person: Person): Boolean = person.age >= 18 | ||
} | ||
|
||
object BooleanMatcher { | ||
val person = Person("Charlie", 17) | ||
|
||
val adultResult = person match { | ||
case Adult() => s"${person.name} is an adult" | ||
case _ => s"${person.name} is not an adult" | ||
} | ||
|
||
println(adultResult) | ||
} | ||
|
||
|
||
|
||
// Variadic Extractors | ||
// Add cases for exceptions | ||
// | ||
// Adding some warning test cases | ||
// - | ||
|
||
object VariadicExtractor { | ||
// Define an unapply method that takes a List and returns an Option of Seq | ||
def unapplySeq[A](list: List[A]): Option[Seq[A]] = Some(list) | ||
} | ||
|
||
object PatternMatchExample extends App { | ||
def describeList(list: List[Int]): String = list match { | ||
case VariadicExtractor(1, 2, rest @ _*) => | ||
s"Starts with 1, 2 followed by: ${rest.mkString(", ")}" | ||
case VariadicExtractor(1, rest @ _*) => | ||
s"Starts with 1 followed by: ${rest.mkString(", ")}" | ||
case VariadicExtractor(first, second, rest @ _*) => | ||
s"Starts with $first, $second followed by: ${rest.mkString(", ")}" | ||
case VariadicExtractor(single) => | ||
s"Only one element: $single" | ||
case VariadicExtractor() => | ||
"Empty list" | ||
case _ => | ||
"Unknown pattern" | ||
} | ||
|
||
// Test cases | ||
println(describeList(List(1, 2, 3, 4, 5))) // Output: Starts with 1, 2 followed by: 3, 4, 5 | ||
println(describeList(List(1, 3, 4, 5))) // Output: Starts with 1 followed by: 3, 4, 5 | ||
println(describeList(List(2, 3, 4, 5))) // Output: Starts with 2, 3 followed by: 4, 5 | ||
println(describeList(List(1))) // Output: Only one element: 1 | ||
println(describeList(List())) // Output: Empty list | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a refactoring or a bug fix? We can remove the commented code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a refactor. I forgot to remove it after I tested the code to see if it still worked.
Side note, I'll make sure rebase next time 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, it's easy to rebase and squash --- you can force push to the PR branch.