data-class icon indicating copy to clipboard operation
data-class copied to clipboard

Do not generate `withField` methods if they are already defined

Open hamnis opened this issue 6 years ago • 1 comments
trafficstars

@data class Foo(bar: String, baz: Int) {
    def withBar(bar: String): Foo = new Foo(bar)
}

does not work, maybe this should?

I would expect withBaz to be generated, but not withBar

hamnis avatar Oct 08 '19 20:10 hamnis

I agree this would be convenient, but it's kind of tricky to achieve in practice. Trickier than toString, equals, hashCode, etc., which already do that.

Because users may decide to add extra convenient with* methods, refering the generated ones, like

import java.io.File
@data class Foo(file: File) {
  def withFile(file: String): Foo =
    withFile(new File(file))
}

My understanding is that the macros of data-class run before the type checker, so in that case we have no way of knowing for sure if a withFile method replaces the generated one or is just an extra override.

But maybe adding a @nosetter annotation, to disable generating with* methods for particular fields, would work, like

@data class Foo(@nosetter bar: String, baz: Int) {
  def withBar(bar: String): Foo = ???
}

alexarchambault avatar Oct 08 '19 21:10 alexarchambault