- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Using type aliases with pattern matching
Mon, 2010-08-30, 08:51
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
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
Mon, 2010-08-30, 09:37
#2
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:
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
Mon, 2010-08-30, 13:47
#3
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
Mon, 2010-08-30, 14:17
#4
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:
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.it depends when you look at it -- after erasure or before
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
Mon, 2010-08-30, 14:27
#5
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
Mon, 2010-08-30, 14:47
#6
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
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: