- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
bug with case class objects and "normal objects"
Sun, 2012-01-01, 16:43
I found a bug:
Welcome to Scala version 2.10.0.dev-1340-gd1e3b46 (OpenJDK 64-Bit Server
VM, Java 1.6.0_23).
Type in expressions to have them evaluated.
Type :help for more information.
scala> case class X(i: Int)
defined class X
scala> 1 to 5 map X
res0: scala.collection.immutable.IndexedSeq[X] = Vector(X(1), X(2),
X(3), X(4), X(5))
scala> object Y{def apply(i: Int)=new Y(i)};class Y(val i: Int)
defined module Y
defined class Y
scala> 1 to 5 map Y
:9: error: type mismatch;
found : Y.type (with underlying type object Y)
required: Int => ?
1 to 5 map Y
^
The compiler generated object of the case class is handled differently
by the compiler. Is there already a bug report?
Sun, 2012-01-01, 21:01
#2
Re: Re: bug with case class objects and "normal objects"
On Sun, Jan 01, 2012 at 10:26:33AM -0800, rkuhn wrote:
> your object Y is NOT a Function1
>
>
> > scala> 1 to 5 map Y
> > :9: error: type mismatch;
> > found : Y.type (with underlying type object Y)
> > required: Int => ?
> > 1 to 5 map Y
> > ^
Yes, as Roland says, you need a Function1:
scala> object Y { def apply(i:Int) = (i, 99) }
defined module Y
scala> 1 to 3 map Y
:9: error: type mismatch;
found : Y.type (with underlying type object Y)
required: Int => ?
1 to 3 map Y
^
scala> 1 to 3 map (Y.apply _)
res1: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,99), (2,99), (3,99))
So, if you partially-apply the Y.apply method, you get an instance of
Function1 which you can use in the map.
Mon, 2012-01-02, 00:31
#3
Re: bug with case class objects and "normal objects"
On Sun, Jan 1, 2012 at 7:43 AM, Antoras <mail@antoras.de> wrote:
1 to 5 map Y
In addition to the answers already given, this is IMO the high-aesthetics way:
1 to 5 map (x => Y(x))
Mon, 2012-01-02, 00:51
#4
Re: bug with case class objects and "normal objects"
On Mon, Jan 2, 2012 at 12:27 AM, Paul Phillips <paulp@improving.org> wrote:
On Sun, Jan 1, 2012 at 7:43 AM, Antoras <mail@antoras.de> wrote:1 to 5 map Y
In addition to the answers already given, this is IMO the high-aesthetics way:
1 to 5 map (x => Y(x))
I see your aesthetics and raise you a lipstick:
1 to 5 map Y.apply
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang
Mon, 2012-01-02, 01:01
#5
Re: Re: bug with case class objects and "normal objects"
Am 01.01.2012 19:26, schrieb rkuhn:
That is totally true. I didn't realize the dependency to Function1 even though I decompiled the bytecode. I'm sorry for this flaw. I learned something new.
scala> :javap X$
Compiled from "<console>"
public final class X$ extends scala.runtime.AbstractFunction1 implements scala.ScalaObject,scala.Serializable{
public static final X$ MODULE$;
public static {};
public final java.lang.String toString();
public scala.Option unapply(X);
public X apply(int);
public java.lang.Object readResolve();
public java.lang.Object apply(java.lang.Object);
public X$();
}
scala> 1 to 5 map X
res0: scala.collection.immutable.IndexedSeq[X] = Vector(X(1), X(2),
X(3), X(4), X(5))
map requires some Function1, which object X implements
That is totally true. I didn't realize the dependency to Function1 even though I decompiled the bytecode. I'm sorry for this flaw. I learned something new.
Mon, 2012-01-02, 02:01
#6
Re: Re: bug with case class objects and "normal objects"
No need to be sorry, especially for learning! You are always welcome to do that.
Regards,
Roland
Mon, 2012-01-02, 14:51
#7
Re: Re: bug with case class objects and "normal objects"
On Sun, Jan 1, 2012 at 6:26 PM, rkuhn wrote:
> your object Y is NOT a Function1
So make it one,
scala> object Y extends (Int => Y) { def apply(i: Int) = new Y(i) } ;
class Y(val i: Int)
defined module Y
defined class Y
scala> 1 to 5 map Y
res0: scala.collection.immutable.IndexedSeq[Y] = Vector(Y@31caa1f,
Y@4ee4dad1, Y@12d9f7ce, Y@241bff0d, Y@2a05c100)
Cheers,
Miles
Am Sonntag, 1. Januar 2012 16:43:52 UTC+1 schrieb Antoras:
do not be hasty, young padawan!
scala> :javap X$
Compiled from "<console>"
public final class X$ extends scala.runtime.AbstractFunction1 implements scala.ScalaObject,scala.Serializable{
public static final X$ MODULE$;
public static {};
public final java.lang.String toString();
public scala.Option unapply(X);
public X apply(int);
public java.lang.Object readResolve();
public java.lang.Object apply(java.lang.Object);
public X$();
}
map requires some Function1, which object X implements
your object Y is NOT a Function1
As I said: too hasty ;-)
Regards,
Roland