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

Sealed wither interface? #27

Closed
bowbahdoe opened this issue Feb 5, 2021 · 13 comments · Fixed by #49
Closed

Sealed wither interface? #27

bowbahdoe opened this issue Feb 5, 2021 · 13 comments · Fixed by #49
Labels

Comments

@bowbahdoe
Copy link

Its still down the line (Java 17, most likely), but it would be nice to have the RecordName.With generated interface be sealed so that only the record can implement it

@Randgalt
Copy link
Owner

Randgalt commented Feb 5, 2021

Yeah - could do. I'll look into it.

@Randgalt Randgalt added the enhancement New feature or request label Feb 5, 2021
@Randgalt
Copy link
Owner

Randgalt commented Feb 7, 2021

It makes more sense to do this for Java 17 when the early builds come out. We wouldn't want to force people to add --enable-preview for Java 16.

@z1065485649
Copy link

I can't find the class nameandagebuilder under the next idea because I didn't install a plug-in. How do I deal with this

@Randgalt
Copy link
Owner

I can't find the class nameandagebuilder under the next idea because I didn't install a plug-in. How do I deal with this

NameAndAgeBuider? That's a generated class. This is an annotation processor that generates Java files. No plugin needed.

@z1065485649
Copy link

Hello, but I compiled and reported errors with idea. He couldn't find the generated class. How to deal with this

@Randgalt
Copy link
Owner

@z1065485649 NameAndAgeBuilder is just an example in the documentation. There is no NameAndAge class in this project. The documentation is showing what the results would be if you had a NameAndAge class.

@Randgalt
Copy link
Owner

Hello, but I compiled and reported errors with idea. He couldn't find the generated class. How to deal with this

If you have errors or problems please create an Github Issue and I'll see if I can help. Post your project somewhere or some example files.

@Randgalt
Copy link
Owner

After investigating this I don't think this is worthwhile. The annotation processor would have to add the permits clause and it would be complicated and not add much value.

@danp11
Copy link

danp11 commented Jun 24, 2021

Hi again Jordan

I hope you don't mind that I ask you to reopen this "issue".

Building systems/good software is IMHO alot about designing the domain model. (I'm a decent Java programmer, but old enough to have appreciated a good old and clever database design :-))

And as records is more about composition than inheritance this ticket would be gold to keep open IMHO. When Java 17 is out we can have another discussion about the reporter's initial proposal. My two cents is that this makes sense.

Thanks for your good work and the exciting last days commits :-)
Nice weekend,

/Dan 

@Randgalt
Copy link
Owner

Randgalt commented Jun 25, 2021

I'll re-open for a future enhancement. It's too hard to do this right now. Javapoet doesn't support it all. I played around with hacking it into the code but it's far too ugly. When https://github.com/square/javapoet supports Java 16 features maybe we can add this.

@Twisol
Copy link

Twisol commented Jul 13, 2021

Is there a way to sidestep this entirely, perhaps, by adding methods matching the record's accessors to the generated interface? This would ostensibly avoid the need for _downcast entirely, and make it unproblematic (if unconventional) to implement the interface on a different class.

Tweaking the example generated code in the README:

public interface With {
    String name();
    int age();

    default NameAndAgeBuilder with() {
        return NameAndAgeBuilder.builder(new NameAndAge(name(), age()));
    }

    default NameAndAge withName(String name) {
        return new NameAndAge(name, age());
    }

    default NameAndAge withAge(int age) {
        return new NameAndAge(name(), age);
    }
}

These methods would be automatically implemented by the record, so no actual change to client code is needed.

@bowbahdoe
Copy link
Author

@Twisol Thats less cool, I veto

@Randgalt
Copy link
Owner

Hmm - I didn't think of that! That's interesting. I'll play around with that when I get a chance.

Randgalt added a commit that referenced this issue Jul 16, 2021
Great suggestion from @Twisol. There's no need for the downcasting
if record component methods are added to the Wither interface.

Closes #27
Randgalt added a commit that referenced this issue Jul 16, 2021
Great suggestion from @Twisol. There's no need for the downcasting
if record component methods are added to the Wither interface.

Closes #27
Randgalt added a commit that referenced this issue Aug 4, 2021
Great suggestion from @Twisol. There's no need for the downcasting
if record component methods are added to the Wither interface.

Closes #27
Randgalt added a commit that referenced this issue Oct 7, 2021
Great suggestion from @Twisol. There's no need for the downcasting
if record component methods are added to the Wither interface.

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

Successfully merging a pull request may close this issue.

5 participants