Implementing object values in nested trait

18 views
Skip to first unread message

gobagoo

unread,
Sep 2, 2011, 2:15:42 PM9/2/11
to scala-user
I was wondering if something like this would be possible, basically
overriding a value of a nested object. Is there a reason why this is
not allowed?

object Test1 extends Application {

trait Trait1 {
object Obj2 extends Trait2
trait Trait2 {
val y : Int
}
}

class Obj1 extends Trait1 {
object Obj2 {
val y = 2
}
}

println((new Obj1).Obj2.y)
}

/Users/atistler/Projects/test1.scala:5: error: object creation
impossible, since value y in trait Trait2 of type Int is not defined
object Obj2 extends Trait2
^
/Users/atistler/Projects/test1.scala:13: error: overriding object Obj2
in trait Trait1 of type object Obj1.this.Obj2;
object Obj2 cannot be used here - classes and objects cannot be
overridden
object Obj2 {
^
two errors found

Nils Kilden-Pedersen

unread,
Sep 2, 2011, 4:04:34 PM9/2/11
to gobagoo, scala-user
On Fri, Sep 2, 2011 at 1:15 PM, gobagoo <atis...@gmail.com> wrote:
I was wondering if something like this would be possible, basically
overriding a value of a nested object.  Is there a reason why this is
not allowed?

object Test1 extends Application {

 trait Trait1 {
    object Obj2 extends Trait2
    trait Trait2 {
      val y : Int
    }
 }

Certainly the above is wrong. Trait2 has abstract val y, and Obj2 does not provide it when extending Trait2.
 

Johannes Huning

unread,
Sep 2, 2011, 6:04:57 PM9/2/11
to scala...@googlegroups.com
On 09/02/2011 08:15 PM, gobagoo wrote:
> I was wondering if something like this would be possible, basically
> overriding a value of a nested object. Is there a reason why this is
> not allowed?

It is not. Also see http://www.scala-lang.org/node/4935

--
Johannes

signature.asc

Adam Tistler

unread,
Sep 2, 2011, 9:43:39 PM9/2/11
to Nils Kilden-Pedersen, scala-user
Thanks for the help....again:)  I think you are the same guy who does the ORBroker project...anyways

object Test1 extends Application {

  trait Trait1 {
     val x: String
     val Obj2 : Trait2
     trait Trait2 {
       val y : Int
     }
  }

  class Obj1 extends Trait1 {
    val x = "Hello"
    object Obj2 extends Trait2 {
      val y = 2
    }
  }

  println((new Obj1).Obj2.y)
}


This seems to do what I am looking for.  I am surprised that this line actually actually works:

object Obj2 extends Trait2 

I would think that Trait2 would not visible from inside Obj1, since Trait2 is defined inside Trait1....hmm.  To me the original syntax makes more sense, why not allow for an unimplemented object in a trait that needs to be implemented in a subclass??  

Daniel D.

unread,
Sep 3, 2011, 6:30:15 AM9/3/11
to scala-user


On 3 Sep., 03:43, Adam Tistler <atist...@gmail.com> wrote:

>
> object Obj2 extends Trait2
>
> I would think that Trait2 would not visible from inside Obj1, since Trait2 is defined inside Trait1....hmm.  To me the original syntax makes more sense, why not allow for an unimplemented >object in a trait that needs to be implemented in a subclass??  

Of course Trait2 is visible in Obj1, because Obj1 inherits from
Trait1, wherein Trait2 is defined.
To the second point, an object is by definition already an
implementation and an instance. If you look for something abstract use
abstract classes or traits.
Reply all
Reply to author
Forward
0 new messages