score:0
yes, take a look at "matching on case classes" here: http://docs.scala-lang.org/tour/pattern-matching.html#matching-on-case-classes
from there:
abstract class notification
case class email(sender: string, title: string, body: string) extends notification
case class sms(caller: string, message: string) extends notification
case class voicerecording(contactname: string, link: string) extends notification
def shownotification(notification: notification): string = {
notification match {
case email(email, title, _) =>
s"you got an email from $email with title: $title"
case sms(number, message) =>
s"you got an sms from $number! message: $message"
case voicerecording(name, link) =>
s"you received a voice recording from $name! click the link to hear it: $link"
}
}
update: it's hard to know what you're looking for without a more concrete example... maybe this:
trait frontman
case object mickjagger extends frontman
trait band
case object rollingstones extends band
frontman match {
case mickjagger => rollingstones
}
update, slightly cleaner:
abstract class band (val frontman: string)
object rollingstones extends band(frontman = "mick jagger")
val tobematched = "mick jagger"
tobematched match {
// no need for if guard
case rollingstones.frontman => println("we got the rolling stones singer!")
}
the if-guard is not needed since you can use an identifier directly in the pattern match. but it requires that it be a stable identifier (ex: val, object, literal... not var or def). so the original def frontman
would not have worked. if the identifier had been just a val
without an object prefix, then it would have needed backticks to differentiate it from a pattern variable.
Source: stackoverflow.com
Related Query
- Scala: Pattern Matching Based on Self Defined Types
- Scala pattern matching distinguishing path dependent types depends on scope instances defined in?
- Scala self types in pattern matching
- Pattern matching structural types in Scala
- Pattern matching on nested types in Scala
- Scala pattern matching against something of multiple types
- Why does this snippet with pattern matching and higher kinded types no longer compile in Scala 2.12?
- how can i find a key in a map based on a pattern matching in scala
- Scala pattern matching fails on recursive types in 2.10
- Scala : Pattern matching against generic list types
- Scala - union types in pattern matching
- match a String based on regex pattern matching scala
- scala pattern matching on object type and method accepting argument of many types
- Existential types and pattern matching in Scala
- Scala Pattern matching with tuple types
- separate scala list based on matching pattern
- match a timestamp based on regex pattern matching scala
- How is pattern matching in Scala implemented at the bytecode level?
- Why does pattern matching in Scala not work with variables?
- Scala multiple type pattern matching
- Scala pattern matching on sequences other than Lists
- Scala pattern matching confusion with Option[Any]
- How does Pattern Matching in Scala overcome duplication that switch case causes?
- What exactly did Scala improve with pattern matching in 2.10?
- Pattern matching on generic type in Scala
- Pattern matching against Scala Map type
- matching types in scala
- Is there any fundamental limitations that stops Scala from implementing pattern matching over functions?
- Scala pattern matching against URLs
- Scala Pattern Matching with Sets
More Query from same tag
- Why is it illegal to call mean on this 3x2 matrix of doubles in Scala Breeze?
- How can I pass a type as a parameter in scala?
- Back-quote identifier within interpolated string
- Getting the error "play-iteratees_2.10 not found" after adding the reactivemongo
- Create a single collection from multiple collections in a way that maximises distance between elements of the same initial collection
- How do I load configuration file on startup
- How to stop Source.tick in the Actor?
- Parallel map operations?
- java.lang.ClassCastException: java.lang.String cannot be cast to scala.collection.Seq
- Can a Scala program be compiled to run on any JVM, without having Scala installed on the given machine?
- Scala Implicit Conversion
- Slick 2 fails to compile query which returns tuple
- Spark-shell backspace is printing '$<3>' on screen
- Flink two different packages for ProcessWindowFunction
- How to do fast prefix string matching in Scala
- Cannot mutate var in an object in scala?
- project/Dependencies.scala not accessible from plugins.sbt
- Alternate Use to 'return' in Scala?
- How to find a string in a list of case class
- How does Play recognize a particular websocket message?
- Slick, H2 insert query auto increment ID
- How to read messages from ZHub via ZStream?
- How to iterate over range with negative step?
- Scala internal compiler error (possibly?)
- ResultSetExtractorException in SQL-Interpolation
- scala map / type instantiation
- Do I need a JAR file to run a Flink application?
- Imported Scala Project has no run option
- Run a read-only test in Spark
- No instance of play.api.libs.json.Format is available for akka.actor.typed.ActorRef[org.knoldus.eventSourcing.UserState.Confirmation]