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

bug with case class objects and "normal objects"

7 replies
Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.

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?

roland.kuhn
Joined: 2011-02-21,
User offline. Last seen 35 weeks 3 days ago.
Re: bug with case class objects and "normal objects"


Am Sonntag, 1. Januar 2012 16:43:52 UTC+1 schrieb Antoras:
I found a bug:

do not be hasty, young padawan!
 

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> :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
 

scala> object Y{def apply(i: Int)=new Y(i)};class Y(val i: Int)
defined module Y
defined class Y


your object Y is NOT a Function1
 

scala> 1 to 5 map Y
<console>: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?


As I said: too hasty ;-)

Regards,

Roland
d_m
Joined: 2010-11-11,
User offline. Last seen 35 weeks 2 days ago.
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.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: bug with case class objects and "normal objects"


On Sun, Jan 1, 2012 at 7:43 AM, Antoras <mail [at] antoras [dot] 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))
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: bug with case class objects and "normal objects"


On Mon, Jan 2, 2012 at 12:27 AM, Paul Phillips <paulp [at] improving [dot] org> wrote:


On Sun, Jan 1, 2012 at 7:43 AM, Antoras <mail [at] antoras [dot] 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
Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: Re: bug with case class objects and "normal objects"
Am 01.01.2012 19:26, schrieb rkuhn:

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.
roland.kuhn
Joined: 2011-02-21,
User offline. Last seen 35 weeks 3 days ago.
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

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
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

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