We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
For example, in Option interface, we want to make sure that only two implementations of Option are available, Some and None.
Right now I'm doing a trick to forbid other classes may implement the Option interface.
@HigherKind public interface Option extends OptionOf<T> { OptionModule getModule(); final class Some implements Option<T> { //... public OptionModule getModule() { throw new UnsupportedOperationException(); } } final class None implements Option<T> { //... public OptionModule getModule() { throw new UnsupportedOperationException(); } } } interface OptionModule {}
OptionModule is not visible outside, so you cannot implement this interface outside this package.
OptionModule
But I think it's possible to implement the same using annotation processing.
@HigherKind(sealed = true) public interface Option extends OptionOf<T> { //.. final class Some implements SealedOption<T> { //... } final class None implements SealedOption<T> { //... } }
The this code will be generated
public interface OptionOf<T> extends Kind<Option_, T> { SealedOption<T> youShallNotPass(); //... } // package private interface SealedOption<T> extends Option<T> { @Override default SealedOption<T> youShallNotPass() { throw new UnsupportedOperationException(); } }
This way, if you try to implement Option outside, the compiler will fail, because SealedOption<T> is not visible.
SealedOption<T>
The text was updated successfully, but these errors were encountered:
e264dbe
tonivade
No branches or pull requests
For example, in Option interface, we want to make sure that only two implementations of Option are available, Some and None.
Right now I'm doing a trick to forbid other classes may implement the Option interface.
OptionModule
is not visible outside, so you cannot implement this interface outside this package.But I think it's possible to implement the same using annotation processing.
The this code will be generated
This way, if you try to implement Option outside, the compiler will fail, because
SealedOption<T>
is not visible.The text was updated successfully, but these errors were encountered: