data-class
data-class copied to clipboard
Do not generate `withField` methods if they are already defined
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
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 = ???
}