-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Opt-in autocast for integers and floats #11431
Opt-in autocast for integers and floats #11431
Conversation
Co-authored-by: Sijawusz Pur Rahnama <[email protected]>
Some possible additional specs: def foo(x : Int64 | UInt64)
x
end
def bar
1_i32
end
foo(bar) # should fail def foo(x : Int64 | String)
x
end
def bar
1_i32 || ""
end
foo(bar) # should be 1_i64 on codegen |
@HertzDevil good examples, yet the second one fails (I don't honestly know why, since the mechanism is the same as for e.g. symbol autocast) |
When I originally did this PR, I didn't add code to support that second example. So if you used that same code, it won't work. And, for now, I wouldn't worry about that case. Autocasting when unions are involved doesn't work at all, not even in the current autocasting: def foo(x : Int64 | String)
puts x, typeof(x)
end
foo(1 || "a") Gives:
|
Would be nice to add these examples as |
@Sija I don't think pending examples make sense here. That's just an independent, additional feature which isn't even completely well understood and finalized. We don't add pending specs for that. Instead, there should be an issue to track this as a feature request. |
@beta-ziliani @straight-shoota Could we make this opt-out? That is, enable it by default with a way to stop this behavior if things go wrong. Why?
|
Sounds reasonable to me 👍 |
Sure, makes sense to enable this for evaluation on nightly releases. We can reconsider before finalizing 1.3 whether we want to release it as opt-in. Until then we have at least a couple of weeks for testing. |
I definitely prefer Opt-Out. We've been using autocast in quite a few libraries for a year or more and now it doesn't work. |
How can you be using this feature for a year? It has just been implemented and merged a couple of weeks ago. |
A couple years ago a feature was added which allowed for UInt8 to be passed in to params which were larger like UInt64. I have a couple lines of code which used to compile that don't now. It's nothing that can't be easily fixed though. UPDATE: What used to work as of a couple years ago is this. It also still works so I guess I had assumed that the feature from 2019 went further than it did. Sorry. Either way I really like the idea of having it by default and opting out since this is the expectation people coming from C have. |
@Blacksmoke16 what do you mean here? |
@beta-ziliani Just that @elorest said:
So was assuming the opt-in also made that existing feature opt-in. |
Fixes #9565
Closes #9618
For the moment, it's guarded behind a compiler flag, in order to give us a time to test it. The following program compiles with
crystal build -Dnumber_autocast
:UPDATE: This feature became opt-out in #11529