-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Try to refactor convertTo method for improved type handling with union types #24585
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
Draft
noti0na1
wants to merge
1
commit into
scala:main
Choose a base branch
from
dotty-staging:fix-24571
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.
+34
−40
Draft
Changes from all commits
Commits
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
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.
This is the part where I would say I don't agree with and why I say it is a can of worms.
What if we have
val x: Byte | Short = 0, maybe this should be aShortinstead of aByte(take the largest type). This is something that needs to be decided and what approach we will take.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.
Do you have a concrete example why this matters? I think if there is any ambiguity inside the expected union type, it's user's responsibility to specify a type first.
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.
You could say why does it matter what is the type of the constant of the rhs.
It matters because of macros first. Second, we are thinking about how to encode
Byte,Short,... literals in the language. We need to then know that literal, what would be its exact type.Third,
transparent inline def foo: Byte | Short = 0, since this is a transparent inline, we cannot just ignore the type information to see that it was aByte | Short, we need to know what kind of0and we can clearly not say it was anIntby stripping the type information completely.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.
We were writing at the same time 😅
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, maybe the user should not write this and in my opinion they shouldn't but Scala 3 has a specification and even if this is a very niche edge case that no one should write, it is still important to specify it and understand all the possible designs we could have chosen and chose one wisely (because once we do, there is no changing 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.
I just had a chat with @odersky about this and he thinks that this should not be fixed at all.
Even
val x: Byte | String = 0should fail.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 would need at least a SIP. Feature interaction is a problem here. What if there was an implicit conversion from
InttoByte | String? Then that would have been picked before but now it would be bypassed.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.
How about: it should fail if there are different ways to convert according to the union type; success if there is only one result.
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 completely agree on this.
That's also something to consider if we were to pursue these improvements.
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.
That's also another way of handling the situation. My point is (and was this morning too), this is not as simple as opening a PR with one of these solutions. There is many factors to take into account and this will always result into a change of spec (so a SIP as Martin mentioned too). The
T | Nullwas an obvious one that needed to be fixed and we did, so pressure is off.