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

why case class and its Companion object definition order matters?

4 replies
Sadek Drobi
Joined: 2010-09-22,
User offline. Last seen 42 years 45 weeks ago.
Is there any reason for which this
scala> {                                                         | object FF { def apply(id:Int,name:String):FF= FF(name) }     | case class FF(name:String)                                    | FF(1,"BB")                                                   | }
should be different from this?
scala> {     | case class FF(name:String)      | object FF { def apply(id:Int,name:String):FF= FF(name) }     | FF(1,"BB")     | }

--
www.sadekdrobi.com
ʎdoɹʇuǝ
Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: why case class and its Companion object definition order ma

On Sunday April 24 2011, Sadek Drobi wrote:
> Is there any reason for which this
>
> scala> {
>
> | object FF { def apply(id:Int,name:String):FF= FF(name) }
> | case class FF(name:String)
> | FF(1,"BB")
> | }
>
> should be different from this?
>
> scala> {
>
> | case class FF(name:String)
> | object FF { def apply(id:Int,name:String):FF= FF(name) }
> | FF(1,"BB")
> | }

REPL.

Any given sequence of Scala source-code bytes do not have the same
semantics in the REPL they do when compiled by scalac.

If you compiled those, there'd be no difference.

When you submit them to the REPL, the latter expression is in a
different scope than the earlier one and that scope is nested within
the earlier expression's scope.

Randall Schulz

Sadek Drobi
Joined: 2010-09-22,
User offline. Last seen 42 years 45 weeks ago.
Re: why case class and its Companion object definition order ma
Even if I am submitting them all at once in a block? It seems to me that I had the problem outside the REPL but I can be mistaken.
On Mon, Apr 25, 2011 at 2:03 AM, Randall R Schulz <rschulz@sonic.net> wrote:
On Sunday April 24 2011, Sadek Drobi wrote:
> Is there any reason for which this
>
> scala> {
>
>      | object FF { def apply(id:Int,name:String):FF= FF(name) }
>      | case class FF(name:String)
>      | FF(1,"BB")
>      | }
>
> should be different from this?
>
> scala> {
>
>      | case class FF(name:String)
>      | object FF { def apply(id:Int,name:String):FF= FF(name) }
>      | FF(1,"BB")
>      | }

REPL.

Any given sequence of Scala source-code bytes do not have the same
semantics in the REPL they do when compiled by scalac.

If you compiled those, there'd be no difference.

When you submit them to the REPL, the latter expression is in a
different scope than the earlier one and that scope is nested within
the earlier expression's scope.


Randall Schulz



--
www.sadekdrobi.com
ʎdoɹʇuǝ
Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: why case class and its Companion object definition order ma

On Sunday April 24 2011, Sadek Drobi wrote:
> Even if I am submitting them all at once in a block? It seems to me
> that I had the problem outside the REPL but I can be mistaken.

These two files both compile without a peep from scalac:

-==- OCC.scala -==-
object FF { def apply(id:Int,name:String):FF= FF(name) }
case class FF(name:String)

object OCC
{
def main(args: Array[String]) {
FF(1,"BB")
}
}
-==- OCC.scala -==-

-==- CCO.scala -==-
case class FF(name:String)
object FF { def apply(id:Int,name:String):FF= FF(name) }

object CCO {
def main(args: Array[String]) {
FF(1,"BB")
}
}
-==- CCO.scala -==-

Randall Schulz

Sadek Drobi
Joined: 2010-09-22,
User offline. Last seen 42 years 45 weeks ago.
Re: why case class and its Companion object definition order ma
Cool, I guess my other problem was about having companion and case class in different scopes in which case one would obviously shadow the other.
Thanks Randall.

On Mon, Apr 25, 2011 at 2:46 AM, Randall R Schulz <rschulz@sonic.net> wrote:
On Sunday April 24 2011, Sadek Drobi wrote:
> Even if I am submitting them all at once in a block? It seems to me
> that I had the problem outside the REPL but I can be mistaken.

These two files both compile without a peep from scalac:

-==- OCC.scala -==-
object FF { def apply(id:Int,name:String):FF= FF(name) }
case class FF(name:String)

object OCC
{
   def main(args: Array[String]) {
       FF(1,"BB")
   }
}
-==- OCC.scala -==-


-==- CCO.scala -==-
case class FF(name:String)
object FF { def apply(id:Int,name:String):FF= FF(name) }

object CCO {
   def main(args: Array[String]) {
       FF(1,"BB")
   }
}
-==- CCO.scala -==-


Randall Schulz



--
www.sadekdrobi.com
ʎdoɹʇuǝ

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