-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix "Typer regression for sum type derivation in paoloboni/binance-scala-client" #24195
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
Open
zielinsky
wants to merge
2
commits into
scala:main
Choose a base branch
from
zielinsky:i24093v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| -- [E197] Potential Issue Warning: tests/warn/i24093.scala:18:10 ------------------------------------------------------- | ||
| 18 | new StringConverter[T]: // warn | ||
| | ^ | ||
| | New anonymous class definition will be duplicated at each inline site | ||
| | | ||
| | longer explanation available when compiling with `-explain` | ||
| -- [E197] Potential Issue Warning: tests/warn/i24093.scala:34:4 -------------------------------------------------------- | ||
| 34 | new QueryParamsConverter[T] { // warn | ||
| | ^ | ||
| | New anonymous class definition will be duplicated at each inline site | ||
| | | ||
| | longer explanation available when compiling with `-explain` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import scala.deriving.* | ||
| import scala.compiletime.* | ||
|
|
||
| trait StringConverter[T]: | ||
| def to(t: T): String | ||
|
|
||
| trait QueryParams | ||
| trait QueryParamsConverter[T]: | ||
| def to(t: T): QueryParams | ||
|
|
||
| object QueryParamsConverter { | ||
| extension [T](t: T) | ||
| def toQueryParams(using converter: QueryParamsConverter[T]): QueryParams = converter.to(t) | ||
|
|
||
| inline private def summonStringConverter[T]: StringConverter[T] = summonFrom { | ||
| case converter: StringConverter[T] => converter | ||
| case _ => | ||
| new StringConverter[T]: // warn | ||
| override def to(t: T): String = t.toString | ||
| } | ||
|
|
||
| inline def summonStringConverters[A <: Tuple]: List[StringConverter[?]] = | ||
| inline erasedValue[A] match | ||
| case _: EmptyTuple => Nil | ||
| case _: (t *: ts) => summonStringConverter[t] :: summonStringConverters[ts] | ||
|
|
||
| inline def summonQueryParamsConverters[A <: Tuple]: List[QueryParamsConverter[?]] = | ||
| inline erasedValue[A] match | ||
| case _: EmptyTuple => Nil | ||
| case _: (t *: ts) => summonInline[QueryParamsConverter[t]] :: summonQueryParamsConverters[ts] | ||
|
|
||
| inline given derived[T](using m: Mirror.Of[T]): QueryParamsConverter[T] = { | ||
| val stringConverters = summonStringConverters[m.MirroredElemTypes] | ||
| new QueryParamsConverter[T] { // warn | ||
| override def to(t: T): QueryParams = | ||
| inline m match | ||
| case p: Mirror.ProductOf[T] => ??? | ||
| case s: Mirror.SumOf[T] => | ||
| val _ = summonQueryParamsConverters[m.MirroredElemTypes] | ||
| ??? | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object Test: | ||
| sealed trait FutureOrderCreateParams | ||
| object FutureOrderCreateParams: | ||
| case class LIMIT(quantity: BigDecimal) extends FutureOrderCreateParams | ||
|
|
||
| case class FutureOrderCreateResponse(clientOrderId: String) | ||
|
|
||
| import QueryParamsConverter.* | ||
| def createOrder(orderCreate: FutureOrderCreateParams) = orderCreate.toQueryParams |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we have a fix without this change?
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 think this the main part, this fixes the issue
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.
It fixes the issue because the issue uses
BigDecimal. I'm curious if the same issue could arise when defining a custom class that overridestoStringand define aToExpron it.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.
Yes, it can.
If we override methods from Java, we should require the override to be non-nullary, however, that wouldn't be backward-compatible. Alternatively, the typer could rewrite the override method from nullary to nilary.
What's the reason for the mismatch in the definition of the
toStringmethod in stdlib (e.g.BigIntvsBigDecimal)? I think that we should standardize this.In any case, this change is invisible to developers, fixes the regression, and is consistent with the Java definition.
Uh oh!
There was an error while loading. Please reload this page.
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'm sure it would. I don't remember why I wrote my comment with if.
I agree with that and I tried to make the case a long time ago but I've been told it works as designed. This was the last time I brought this topic in a public comment. Regardless, we should acknowledge the bug with
ToExprand Java overrides in a separate issue and have it more minimized know that we fully understand the root of the issue. When that is done, I would be happy to approve this PR to at least close the regression.