Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not generate withField methods if they are already defined #9

Open
hamnis opened this issue Oct 8, 2019 · 1 comment
Open

Do not generate withField methods if they are already defined #9

hamnis opened this issue Oct 8, 2019 · 1 comment

Comments

@hamnis
Copy link

hamnis commented Oct 8, 2019

@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

@alexarchambault
Copy link
Owner

alexarchambault commented Oct 8, 2019

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 = ???
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants