Scala Programming Language
  1. Scala Programming Language
  2. SI-6408

Universal traits shouldn't allow nested objects

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0
    • Fix Version/s: Scala 2.10.0-M7, Scala 2.10.0
    • Component/s: Misc Compiler
    • Labels:
    • Environment:

      Scala 2.10.0-20120919-113813-b7c108a878

      Based on b9fd3f713d4c0122c1411315ad25049d117df57c from 2.10.x plus latest value class fixes from pull requests 1342 and 1352.

      Description

      According to SIP-15, "Universal traits may have neither fields nor initialization statements." Nested objects are effectively both.

      trait U extends Any {
        def x: Int
        object X {
          lazy val y = { println("x: " + x); x }
          var yy = x
        }
      }
      
      class V(val x: Int) extends AnyVal with U
      
      object W {
        def main(args: Array[String]) {
          val v = new V(3)
          val z = v.X.y
          v.X.yy = 30
          val z2 = v.X.y
          val v2 = new V(4)
          v2.X.yy = 31
          println(z)
          println(z2)
          println(v.X.yy)
          println(v2.X.yy)
        }
      }
      

      Output:

      x: 3
      x: 3
      3
      3
      3
      4
      

      Output without `extends AnyVal`:

      x: 3
      3
      3
      30
      31
      

      I'm not sure if nested classes and traits should be excluded as well, but it might be safer to start by excluding them.

        Activity

        Hide
        Josh Suereth added a comment -

        hey martin, throwing one your way, please punt to whoever is appropriate, thanks!

        Show
        Josh Suereth added a comment - hey martin, throwing one your way, please punt to whoever is appropriate, thanks!
        Show
        Martin Odersky added a comment - https://github.com/scala/scala/pull/1366
        Hide
        Martin Odersky added a comment -

        Greg has taken this one over.

        Show
        Martin Odersky added a comment - Greg has taken this one over.
        Show
        Grzegorz Kossakowski added a comment - Fixed in https://github.com/scala/scala/pull/1443

          People

          • Assignee:
            Grzegorz Kossakowski
            Reporter:
            Mark Harrah
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development