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

unadvertised change to case class weight due to copy method

    Details

      Description

      In scala 2.7, this class had one field. In scala 2.8 and beyond, it has three.

      case class A(x: String)(y: String, z: String) { }
      

      I'm pretty sure that using parameter lists beyond the first has been an advertised way of avoiding field creation for case class parameters. This was undone, since the copy method references all the parameters in all the lists and so fields are created.

      You can somewhat distastefully get around this by inhibiting the creation of the copy method.

      case class A(x: String)(y: String, z: String) {
        private def copy = "no fields for y and z"
      }
      

      This business should at least be documented.

        Issue Links

          Activity

          Hide
          Paul Phillips added a comment -

          For the record, martin says this is a bug, and we should changes things in some fashion to get back to one field.

          Show
          Paul Phillips added a comment - For the record, martin says this is a bug, and we should changes things in some fashion to get back to one field.
          Hide
          Adriaan Moors added a comment -

          original motivation for multiple argument lists for cases classes: don't need field for those args, not relevant for pattern matching

          but, now we have a copy method:
          [ - don't copy those fields?]
          --> eta-expand the copier for the trailing argument lists

          Show
          Adriaan Moors added a comment - original motivation for multiple argument lists for cases classes: don't need field for those args, not relevant for pattern matching but, now we have a copy method: [ - don't copy those fields?] --> eta-expand the copier for the trailing argument lists
          Hide
          Paul Phillips added a comment -

          To clarify adriaan's comment for the record (I'm thinking he hooked the tubes up to his head to catch the runoff) it was proposed, I think with general agreement, that case classes with a single parameter list be treated as before, and those with multiple have a copy method which returns the eta-expansion. An unusually satisfying solution really.

          case class Foo(x: Int)(y: Int, z: Int) {
            <synthetic> def copy(x: Int = this.x) = new Foo(x)(_: Int, _: Int)
          }
          
          Show
          Paul Phillips added a comment - To clarify adriaan's comment for the record (I'm thinking he hooked the tubes up to his head to catch the runoff) it was proposed, I think with general agreement, that case classes with a single parameter list be treated as before, and those with multiple have a copy method which returns the eta-expansion. An unusually satisfying solution really. case class Foo(x: Int)(y: Int, z: Int) { <synthetic> def copy(x: Int = this.x) = new Foo(x)(_: Int, _: Int) }
          Hide
          Adriaan Moors added a comment - - edited

          Martin says: case class ness is only bestowed on the first argument list
          the rest should not be copied. but we still need values for them to call the constructor

          Show
          Adriaan Moors added a comment - - edited Martin says: case class ness is only bestowed on the first argument list the rest should not be copied. but we still need values for them to call the constructor
          Show
          Lukas Rytz added a comment - fixed in https://github.com/scala/scala/commit/40e7cab7a22a8531bdd310bfb57fda51b798c690
          Hide
          Lukas Rytz added a comment -

          need to update the spec

          Show
          Lukas Rytz added a comment - need to update the spec
          Show
          Lukas Rytz added a comment - refined in https://github.com/scala/scala/commit/6c7f2b6460de1aa6d15a6005921ca50e98a54027
          Hide
          Lukas Rytz added a comment -

          closing this, pending spec change in SI-6068

          Show
          Lukas Rytz added a comment - closing this, pending spec change in SI-6068

            People

            • Assignee:
              Lukas Rytz
              Reporter:
              Paul Phillips
            • Votes:
              2 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development