- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Extractors and pattern matching
Sat, 2012-02-18, 16:57
Hi,
if have two problems with extractors in the method main(...) below:
First: ist there any possibility to denote two different unapply
functions like it is shown in the Filter class that don't lead to
ambiguous references?
Second: if I uncomment the second unapply method (and of course the
first case statement in main(...)), how can I use the extractor in a
case clause? I always get an error like:
pattern type is incompatible with expected type; found : object
Test.Manager.MyFilter required: Test.MatchingElement
type mismatch; found : object Test.Manager.MyFilter required:
Test.MatchingElement
object Test
{
trait Match
class Element
class MatchingElement extends Element with Match
class Filter[T <: Element]
{
def unapply(element: T): Boolean = element.isInstanceOf[Match]
def unapply(element: T)(implicit dummy: DummyImplicit):
Option[String] =
(element.isInstanceOf[Match]) match
{
case true => Some("Hello")
case false => None
}
}
object Manager
{
object MyFilter extends Filter[Element]
}
def main(args: Array[String]): Unit =
{
val e = new MatchingElement
e match
{
case Manager.MyFilter(test) => println(test)
case Manager.MyFilter => println("Here")
}
}
}
Sat, 2012-02-18, 22:01
#2
Re: Extractors and pattern matching
Thanks for your help. Yes, I already thought about moving it to
another place...
Am 18.02.2012 18:34, schrieb Trond Olsen:
Best Regards Ka Ter
Am 18.02.2012 18:34, schrieb Trond Olsen:
EF+AozHb9CE8HkEgmqUQyCdYRwA [at] mail [dot] gmail [dot] com" type="cite">Your last question requires an explicit application like case Manager.MyFilter() => println("Here")[1]. Your first question, can't you just create a separate extractor object for that?
[1] http://www.artima.com/pins1ed/extractors.html
On 18 February 2012 16:57, Ka Ter <ter [dot] ka966 [at] googlemail [dot] com" rel="nofollow">ter.ka966@googlemail.com> wrote:
Hi,
if have two problems with extractors in the method main(...) below:
First: ist there any possibility to denote two different unapply
functions like it is shown in the Filter class that don't lead to
ambiguous references?
Second: if I uncomment the second unapply method (and of course the
first case statement in main(...)), how can I use the extractor in a
case clause? I always get an error like:
pattern type is incompatible with expected type; found : object
Test.Manager.MyFilter required: Test.MatchingElement
type mismatch; found : object Test.Manager.MyFilter required:
Test.MatchingElement
object Test
{
trait Match
class Element
class MatchingElement extends Element with Match
class Filter[T <: Element]
{
def unapply(element: T): Boolean = element.isInstanceOf[Match]
def unapply(element: T)(implicit dummy: DummyImplicit):
Option[String] =
(element.isInstanceOf[Match]) match
{
case true => Some("Hello")
case false => None
}
}
object Manager
{
object MyFilter extends Filter[Element]
}
def main(args: Array[String]): Unit =
{
val e = new MatchingElement
e match
{
case Manager.MyFilter(test) => println(test)
case Manager.MyFilter => println("Here")
}
}
}
--
Best Regards
Ka Ter
[1] http://www.artima.com/pins1ed/extractors.html
On 18 February 2012 16:57, Ka Ter <ter.ka966@googlemail.com> wrote: