This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Extractors and pattern matching

2 replies
Ka Ter
Joined: 2011-10-05,
User offline. Last seen 42 years 45 weeks ago.

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")
}
}
}

tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: Extractors and pattern matching
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> 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


Ka Ter
Joined: 2011-10-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Extractors and pattern matching
Thanks for your help. Yes, I already thought about moving it to another place...
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 [dot] ka966 [at] googlemail [dot] 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


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland