-
Notifications
You must be signed in to change notification settings - Fork 454
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
Autofold #448
Autofold #448
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd change the name to @fold
, as we don't have names like @autoderive
. The name of the functions could use a prefix like ifLeft
& ifRight
.
As for the implementation, what happens when a child class has more generic parameters than the parent class? i.e. sealed class Wrap<A>; data class W1<A, B>(val b: B): Wrap<A>
?
Codecov Report
@@ Coverage Diff @@
## master #448 +/- ##
=========================================
Coverage ? 35.62%
Complexity ? 327
=========================================
Files ? 182
Lines ? 4968
Branches ? 532
=========================================
Hits ? 1770
Misses ? 3049
Partials ? 149
Continue to review full report at Codecov.
|
@pakoito |
@pakoito well damn :D
|
@pakoito what would you suggest here? Currently code gen bails with following message, since it impossible to write a fold method in this situation.
|
@nomisRev Add <*, *> to the when clause, but IIRC that means that the caller can assume the type of B and get a runtime error. |
We don't support derivation at the moment anywhere else for kinds of arity higher than 1 so I think this is good as is until we want to support those across the board which we can tackle at once. |
@pakoito I wouldn't add something that will lead to unsafe code. |
@raulraja Doesn't this cause the same kind of conflict PartialFunction had? That's what I mean with unsafe. |
@pakoito it currently fails for all cases where generics cannot be figured out and not just
So no runtime errors can occur from using the generated fold methods. Currently it fails with the message of the invalid code but I will update it proper compile time error message before we merge. |
@pakoito I think #448 (comment) addressed your concern. Can we merge this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Green with NIT: improve the error message.
Code generation that implements extension
fold
functions for sealed classes. Given following snippet.It will generate following code
It does not unwrap
Just
to(A) -> B
because if there would be another subtypedata class Another<A>(val value: A): Maybe<A>()
you would lose type safety over function parameters but on the other hand I might make the code gen smart enough to check that... Thoughts?