-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
+=
and friends should be type-stable
#11971
Comments
Please see extensive past discussions on this topic:
After reading up and there's still a concrete change you'd like to see, feel free to ping here and we can reopen or open up a new issue. |
What would you do here for user types that aren't closed under addition? |
Yes, the key is that |
One could also argue that having Maybe the fact that this kind of request keeps being reported indicates that people think of |
Not all code is performance-critical.
That could be considered a failure of documentation. |
Agreed. This is just an important difference between Julia and other languages that we need to educate people about better. |
What I meant is that the idea that Note that I'm not saying such a thing as an alternative "intuitive behavior" of |
The rewriting of |
@quinnj ,
So the change I would like to see is for literals such as @JeffBezanson , @nalimilan , |
One other interesting question to consider with my proposed change is how to handle constants. Consider
Ideally, I would love to see the return type of this function be the same as the type of I realize as I type this that my issue title is somewhat misleading since mostly I'm worried about how literals/constants are handled since that is what ultimately leads to the type instability in v0.4. |
@nstiurca, I don't think your cases are as confusing in general when you're more familiar with the types of Julia literals. x = UInt8(1)
x += 1
typeof(x) == Int Because you're adding an julia> x = int8(1)
1
julia> y = int8(1)
1
julia> x + y
2
julia> typeof(x + y)
Int64 |
Personally, I think the |
I also think that the |
And for types that aren't closed under addition, |
Runtime error on overflow, yes. I think we have to have that if we adopt the proposal. A negative is that |
I'm not sure I would call it overflow for a custom type that cannot represent the result of addition in its data structure, and needs to promote to a separate higher-level container. |
@timholy Did you mean |
Yes, typo. The proposal to auto-type literals is interesting but not something that can be accomplished with current machinery. As with the |
You also aren't guaranteed to be able to represent literals in the same data structure as every user type. |
Neither would you want to use Anyway, I don't care strongly one way or another, and have other more pressing duties. |
No, and this would be just as slow as
I'm not expecting this data structure to be high-performance, it's intended for convenience. Having |
You can already write the circle area formula like this and get what you want: julia> circ_area(r) = r^2*π/2
circ_area (generic function with 1 method)
julia> circ_area(1.5)
3.5342917352885173
julia> circ_area(1.5f0)
3.5342917f0
julia> circ_area(big(1.5))
3.534291735288517393270473806189440744721815574296994048596812666346293457071964 If the input is |
I don't think this is just a matter of machinery, I suspect it cannot be done coherently without having rules that statically determine the types of all expressions, which would make the language statically typed. |
Steps to reproduce:
Expected results:
Actual results:
Rationale:
Although it makes sense that constant integers default to Int64 (on 64 bit machines), and also that
Uint8 + Int64
yields anInt64
, operations such as+=
,*=
, etc. imply that only the value of the variable on the left-hand-side changes, and not its type. The current type instability of these operators can cause mysterious performance issues and other problems, such as incorrect results if the code relies on unsigned-modulus arithmetic.Discussion
Enforcing type stability for these operators implies that some operations like
/=
shouldn't be defined withInteger
left-hand side. Alternatively, they could be defined in terms of integer division, but that could cause confusion since/
normally promotes toReal
.If there is a compelling reason for keeping the current type-unstable behavior, then perhaps Julia can at least issue a warning when it detects this case.
v0.3 vs v0.4
In v0.3,
+=
always promotes the left-hand-side to machine-length integer, even if both operands are egUint8
. In v0.4, the left-hand-side is type-stable if the right-hand-side is explicitly of the same type; ie,i += 1 % Uint8
is a work-around for incrementingi
wheni
isUint8
. However, I think this is undesirable due to (a) being a weird gotcha of the language people have to watch out for and (b) it being too verbose for such an idiomatic operation as increment.The text was updated successfully, but these errors were encountered: