score:10
the compiler automatically translates your code roughly to this:
def get[t](list: list[any])(implicit tag: classtag[t]) = list.flatmap {
case (element @ tag(_: t)) => some(element)
case _ => none
}
classtag
has an unapply(x: any)
overload that allows it to pattern match on values. i've cleaned up the tree obtained from reify
to only show the relevant parts, but this will show you the full tree:
scala.reflect.runtime.universe.reify {
def get[t](list: list[any])(implicit tag: classtag[t]) = {
list.flatmap {
case element: t => some(element)
case _ => none
}
}
}
also see the scaladoc:
the compiler tries to turn unchecked type tests in pattern matches into checked ones by wrapping a
(_: t)
type pattern asct(_: t)
, wherect
is theclasstag[t]
instance. type tests necessary before calling other extractors are treated similarly.someextractor(...)
is turned intoct(someextractor(...))
ift
insomeextractor.unapply(x: t)
is uncheckable, but we have an instance ofclasstag[t]
.
Source: stackoverflow.com
Related Query
- Implicit ClassTag in pattern matching
- Case Class and Implicit Arguments and Pattern Matching
- ClassTag based pattern matching fails for primitives
- How to make implicit conversion work during pattern matching
- Scala pattern matching on generic type with TypeTag generates a warning while ClassTag not?
- Pattern Matching chooses the wrong case if used with ClassTag
- Implicit conversion from Array to WrappedArray does not happen when pattern matching
- Implicit String to Seq[Char] pattern matching fail
- Using comparison operators in Scala's pattern matching system
- 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
- Pattern matching vs if-else
- Pattern Matching `@` Symbol
- Scala pattern matching on sequences other than Lists
- Pattern matching with conjunctions (PatternA AND PatternB)
- Haskell, Scala, Clojure, what to choose for high performance pattern matching and concurrency
- Scala: short form of pattern matching that returns Boolean
- How does | (pipe) in pattern matching work?
- Scala pattern matching confusion with Option[Any]
- String pattern matching best practice
- 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 to check if string is null or empty
- Pattern matching against Scala Map type
- Pattern matching lists of certain size or greater/less
- scala: convert match statement to pattern matching anonymous function - with values
- Pattern matching structural types in Scala
- Case insensitive pattern matching for strings
More Query from same tag
- Scala: if statement returning Any instead of Unit in a closure
- How can I resolve dependencies when cross-compiling in Scala with sbt?
- Scala add items to ListBuffer inside recursive method
- Lift-mongo-record: Empty JsonObjectField in mongo collection
- Compile errors when attempting to use macros defined in parent eclipse project ("has 2 unimplemented members")
- How to aggregate unknown-typed maps into single map?
- Illegal start of simple expression creating a simple dataframe in spark
- Sum array of vectors by one of fields - scala
- Implementing yield (yield return) using Scala continuations
- Scala sentences parsing using parser-combinators
- Convertin List to Set in Scala
- Higher-kinded type constructors with and without '_'
- Unable to run scala.js on PhantomJSEnv, requiresDOM setting is forced to false
- Running cmd as administrator scala
- jQuery Ajax call to Twitter Finagle server
- Find and Replace item in Scala collection
- Casbah MongoDB Error
- Scalamock : How to specify arbitrary sequence of tuples
- How to call function overloaded by function which return partail function
- Scala println() in zeppelin paragraph doesn't print while regular print() prints well
- Scala SBT how to run both test and it test at the same time
- generic SortByValue based on Numeric
- How do I satisfy IntelliJ IDEA's expectation that my scala class's companion object's declaration be tested?
- Scala: Dealing with optional XML elements and Case Classes
- Extending generic type with Ordered trait makes sbt compiler issue 'diverging implicit expansion for type' error
- Non static method in scala Enum
- scala programming practice with option
- Spark UDF as function parameter, UDF is not in function scope
- How to run Scala REPL commands in ammonite REPL/SHELL?
- how get element from List based on its name?