-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add documentation for Pattern Matching on String
#3118
Conversation
String
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.
Looks good to me, tho maybe we could also document that extractor object is also supported: https://www.scala-lang.org/files/archive/api/2.13.x/scala/StringContext$s$.html#unapplySeq(s:String):Option[Seq[String]]
such that following works (and great for parsing)
scala> object Int:
| def unapply(s: String): Option[Int] = try Some(s.toInt)
| catch case _: NumberFormatException => None
|
// defined object Int
scala> val input: String = "Alice is 25 years old"
val input: String = Alice is 25 years old
scala> val (name, age) = input match
| case s"$name is ${Int(age)} years old" => (name, age)
| case _ => ("No match", -1)
|
val name: String = Alice
val age: Int = 25
@gkepka Would you like to review this PR? |
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.
LGTM
Thank you — a nice addition |
@SethTisue @sjrd
c.c. https://discord.com/channels/632150470000902164/632628489719382036/1286949507128361093
Closes #2687
Btw I didn't build the site locally to test out the changes (I don't use Ruby so lots of hassle to setup things from scratch), but if necessary, I can do local testing.
Feel free to force push to my branch for any obvious improvements (i.e. syntax error, typo etc).