Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Friendseeker
Copy link
Contributor

@Friendseeker Friendseeker commented Nov 24, 2024

@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).

@Friendseeker Friendseeker changed the title Add documentation for Pattern Matching on String Add documentation for Pattern Matching on String Nov 24, 2024
Copy link
Contributor

@scarf005 scarf005 left a 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

_tour/pattern-matching.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String interpolation doc page should cover pattern matching
3 participants