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

Rewrote pattern matching tour #717

Merged
merged 1 commit into from May 12, 2017

Conversation

travissarles
Copy link
Contributor

No description provided.

---

Scala has a built-in general pattern matching mechanism. It allows to match on any sort of data with a first-match policy.
Here is a small example which shows how to match against an integer value:
Pattern matching is a mechanism for checking a value for a pattern. It is a more powerful version of the `switch` statement in Java and it can likewise be used in place of a series of if/else statements.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggested for the first sentence:

Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts.

println(matchTest(3))
import scala.util.Random

val x: Int = new Random().nextInt(10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need new here. Random.nextInt(10) works

## Matching on type
It is also possible to match on type.
```tut
def printType(x: Any): Unit = x match {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ten minutes til @tpolecat shows up and asks if we have to use an example based on Any.

I'm not really sure what the right way to handle this is. The feature exists, it definitely should be included in this section of the tour, and we don't want to say it's always bad, but we don't want to give the impression that matching on Any is normal, either. perhaps up above, before showing case class extraction, you could show something like:

notification match {
    case e: Email => "you've got mail: $e"
    case s: SMS => "someone texted you: $s"
    case v: VoiceRecording => "check your voicemail: $v"
}

and then quickly move on and say "but most of the time, you don't just want to know what type something is, you want to do something with the individual fields, too", and then show the unapply-based version?

leaving the existing progression alone but just coming up with a non-Any based example also seems like a possibility?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@travissarles travissarles force-pushed the pattern-matching branch 2 times, most recently from 5307c73 to 26a3013 Compare February 28, 2017 13:26
@travissarles
Copy link
Contributor Author

@heathermiller ready to merge

@travissarles travissarles moved this from In PR to Ready to Merge in Rewrite Tour of Scala Mar 16, 2017
@travissarles travissarles mentioned this pull request Mar 17, 2017
33 tasks
@travissarles travissarles self-assigned this Mar 21, 2017
@travissarles travissarles merged commit db31f0d into scala:master May 12, 2017
@travissarles travissarles moved this from Ready to Merge to Merged in Rewrite Tour of Scala May 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants