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

Using type aliases with pattern matching

6 replies
marius
Joined: 2008-08-31,
User offline. Last seen 3 years 19 weeks ago.
Hi,
I'm a little puzzled why I can't do something like:
case class A(x, Int, y: Int)
type % = A
val a = A(2, 4)
a match {
  case 2 % 4 => println("ok")  case _ => println("yack")
}
The compiler yields:
error: not found: value %       x match {case 2 % 4 => println("ok"); case _ => println("blah")}                                ^

Br's,Marius
Stephen Tu
Joined: 2010-02-24,
User offline. Last seen 42 years 45 weeks ago.
Re: Using type aliases with pattern matching
the same reason you can't do:

val a = %(2,4)

what you want instead of type is val:

scala> val % = A
%: A.type = <function2>

scala> A(2,4) match {
     | case 2 % 4 => println("YAY")
     | }
YAY

On Mon, Aug 30, 2010 at 12:51 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
Hi,
I'm a little puzzled why I can't do something like:
case class A(x, Int, y: Int)
type % = A
val a = A(2, 4)
a match {
  case 2 % 4 => println("ok")  case _ => println("yack")
}
The compiler yields:
error: not found: value %       x match {case 2 % 4 => println("ok"); case _ => println("blah")}                                ^

Br's,Marius

marius
Joined: 2008-08-31,
User offline. Last seen 3 years 19 weeks ago.
Re: Using type aliases with pattern matching
Neat ... Thanks Stephen !

On Mon, Aug 30, 2010 at 11:04 AM, Stephen Tu <steve33671@gmail.com> wrote:
the same reason you can't do:

val a = %(2,4)

what you want instead of type is val:

scala> val % = A
%: A.type = <function2>

scala> A(2,4) match {
     | case 2 % 4 => println("YAY")
     | }
YAY

On Mon, Aug 30, 2010 at 12:51 AM, Marius Danciu <marius.danciu@gmail.com> wrote:
Hi,
I'm a little puzzled why I can't do something like:
case class A(x, Int, y: Int)
type % = A
val a = A(2, 4)
a match {
  case 2 % 4 => println("ok")  case _ => println("yack")
}
The compiler yields:
error: not found: value %       x match {case 2 % 4 => println("ok"); case _ => println("blah")}                                ^

Br's,Marius


barbara.von-kalm
Joined: 2010-08-30,
User offline. Last seen 42 years 45 weeks ago.
Type mismatch while implementing a method from java-interface

Hello all together,

I need to implement the wollowing method from a java interface (java):

Object process(Object request, String... parameter);

I did it in this way:

def process(request: AnyRef, parameter: String*) : AnyRef = {
 ... //do something
}


But now I get the following error-message:

class SimpleCoRFilterProcessor needs to be abstract, since method process in trait FilterProcessor of
type (x$1: Any,x$2: <repeated...>[java.lang.String])java.lang.Object is not defined


to my mind AnyRef is the corresponding implementation to Object.

Nevertheless I tried to use Any instead of anyRef and this was the result:


final def process(request: Any, parameter: String*) : Object = process(request, parameter: _*)
 
def process(request: AnyRef, parameter: String*) : AnyRef = {                                  
 
   ... // do something
}


And I get the following error:
Multiple markers at this line
                - double definition: method process:(request: AnyRef,parameter: String*)AnyRef and method
                 process:(request: Any,parameter: String*)java.lang.Object at line 11 have same type after erasure:
                 (request: java.lang.Object,parameter: Seq)java.lang.Object
                - Line breakpoint:AbstractCoRFilter [line: 15] - next : Option
                 [com.basfits.fjsh.scala.base.filter.impl.AbstractCoRFilter]


??????????? Is it the same or not?

If  I define the process-method only once this problem occured:

def process(request: Any, parameter: String*) : Any = {                                  ... }


overriding method process in trait FilterProcessor of type (x$1: Any,x$2: <repeated...>[java.lang.String])java.lang.Object;  method process has incompatible type


Do you have any ideas and can help me?


-Barbara
adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: Type mismatch while implementing a method from java-interfa
Hi,
I think this is a bug in the equivalence of Object and Any for Java compatibility -- Any seems to be converted to Object in arguments, but not in the result type.
As a workaround, this should work (as hinted at by the error message you got)
def process(request: Any, parameter: String*) : Object = {  // implementation goes here }
cheersadriaan

On Mon, Aug 30, 2010 at 2:36 PM, <barbara.von-kalm@basf-it-services.com> wrote:

Hello all together,

I need to implement the wollowing method from a java interface (java):

Object process(Object request, String... parameter);

I did it in this way:

def process(request: AnyRef, parameter: String*) : AnyRef = {
 ... //do something
}


But now I get the following error-message:

class SimpleCoRFilterProcessor needs to be abstract, since method process in trait FilterProcessor of
type (x$1: Any,x$2: <repeated...>[java.lang.String])java.lang.Object is not defined

to my mind AnyRef is the corresponding implementation to Object.

Nevertheless I tried to use Any instead of anyRef and this was the result:


final def process(request: Any, parameter: String*) : Object = process(request, parameter: _*)
 
def process(request: AnyRef, parameter: String*) : AnyRef = {                                  
 
   ... // do something
}


And I get the following error:
Multiple markers at this line
                - double definition: method process:(request: AnyRef,parameter: String*)AnyRef and method
                 process:(request: Any,parameter: String*)java.lang.Object at line 11 have same type after erasure:
                 (request: java.lang.Object,parameter: Seq)java.lang.Object
                - Line breakpoint:AbstractCoRFilter [line: 15] - next : Option
                 [com.basfits.fjsh.scala.base.filter.impl.AbstractCoRFilter]


??????????? Is it the same or not?
it depends when you look at it -- after erasure or before 

If  I define the process-method only once this problem occured:

def process(request: Any, parameter: String*) : Any = {                                  ... }


overriding method process in trait FilterProcessor of type (x$1: Any,x$2: <repeated...>[java.lang.String])java.lang.Object;  method process has incompatible type


Do you have any ideas and can help me?


-Barbara

barbara.von-kalm
Joined: 2010-08-30,
User offline. Last seen 42 years 45 weeks ago.
Antwort: Re: Type mismatch while implementing a method from jav

If I do so, I have a problem inside my method: have a look here:

override def process(request: Any, parameter: String*) : Object = {                
        for (n <- next) {
                getMatchingObject(request, parameter: _*) match{
                        case Some(s) => n.process(filter (s, parameter: _*), parameter: _*)
                        case None => n.process(request, parameter: _*)                                
                }
        }
        request
}
 There occures this error:

Multiple markers at this line
        - type mismatch; found : Any required: java.lang.Object Note: Any is not implicitly converted
         to AnyRef. You can safely pattern match x: AnyRef or cast x.asInstanceOf[AnyRef] to do so.
        - type mismatch; found : Any required: java.lang.Object Note: Any is not implicitly converted
         to AnyRef. You can safely pattern match x: AnyRef or cast x.asInstanceOf[AnyRef] to do so.

I am not sure, that it is good to cast request to type AnyRef ....

- Barbara






Adriaan Moors <adriaan.moors@epfl.ch>
Gesendet von: adriaan.moors@gmail.com

30.08.2010 15:12 Bitte antworten an
adriaan.moors@epfl.ch
An barbara.von-kalm@basf-it-services.com Kopie scala@listes.epfl.ch Thema Re: [scala] Type mismatch while implementing a method from java-interface




Hi,

I think this is a bug in the equivalence of Object and Any for Java compatibility -- Any seems to be converted to Object in arguments, but not in the result type.

As a workaround, this should work (as hinted at by the error message you got)

def process(request: Any, parameter: String*) : Object = {
  // implementation goes here
}

cheers
adriaan

On Mon, Aug 30, 2010 at 2:36 PM, <barbara [dot] von-kalm [at] basf-it-services [dot] com> wrote:

Hello all together,

I need to implement the wollowing method from a java interface (java):

Object process(Object request, String... parameter);

I did it in this way:

def process(request: AnyRef, parameter: String*) : AnyRef = {
 ... //do something
}


But now I get the following error-message:

class SimpleCoRFilterProcessor needs to be abstract, since method process in trait FilterProcessor of
type (x$1: Any,x$2: <repeated...>[java.lang.String])java.lang.Object is not defined


to my mind AnyRef is the corresponding implementation to Object.

Nevertheless I tried to use Any instead of anyRef and this was the result:


final def process(request: Any, parameter: String*) : Object = process(request, parameter: _*)
 

def process(request: AnyRef, parameter: String*) : AnyRef = {                                  
 
   ... // do something
}


And I get the following error:
Multiple markers at this line
                - double definition: method process:(request: AnyRef,parameter: String*)AnyRef and method
                 process:(request: Any,parameter: String*)java.lang.Object at line 11 have same type after erasure:
                 (request: java.lang.Object,parameter: Seq)java.lang.Object
                - Line breakpoint:AbstractCoRFilter [line: 15] - next : Option
                 [com.basfits.fjsh.scala.base.filter.impl.AbstractCoRFilter]


??????????? Is it the same or not?
it depends when you look at it -- after erasure or before
 

If  I define the process-method only once this problem occured:

def process(request: Any, parameter: String*) : Any = {                                  ... }


overriding method process in trait FilterProcessor of type (x$1: Any,x$2: <repeated...>[java.lang.String])java.lang.Object;  method process has incompatible type



Do you have any ideas and can help me?


-Barbara

adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: Re: Type mismatch while implementing a method from java-int


On Mon, Aug 30, 2010 at 3:20 PM, <barbara.von-kalm@basf-it-services.com> wrote:
I am not sure, that it is good to cast request to type AnyRef ....
the cast x.asInstanceOf[AnyRef], where x has static type Any is a no-op because AnyRef and Any both erase to Object (the JVM only operates on erased types)
my original explanation was not entirely accurate, see the bug report for more info: http://lampsvn.epfl.ch/trac/scala/ticket/3814
cheersadriaan

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