-
-
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
setfield! does not call convert
#16195
Comments
This is expected since |
The relevant code is in builtins.c. Since it knows the type that it wants at that point, it doesn't seem too hard to insert a call to |
We can change it so that Base exports: setfield!(x, i, v) = Core.setfield!(x, i, convert(fieldtype(x, i), v)) Otherwise, it's pretty hard to insert a call to convert directly from an Intrinsic, since |
New functions `Base.getproperty` and `Base.setproperty!` can be overloaded to change the behavior of `x.p` and `x.p = v`, respectively. This forces inference constant propagation through get/setproperty, since it is very likely this method will yield better information after specializing on the field name (even if `convert` is too big to make us want to inline the generic version and trigger the heuristic normally). closes #16195 (and thus also closes #16226) fix #1974
This issue seems to be not resolved yet. I can reproduce the issue in Julia 1.0, 1.1, and 1.2-DEV.
cc @stevengj |
julia> mutable struct Foo x::Float64; end
julia> foo = Foo(0)
Foo(0.0)
julia> foo.x = 3
3
julia> setproperty!(foo, :x, 5)
5.0
|
|
No, for a start, this was an issue because
There isn't any non-uniform behavior of set-field operation. The dot syntax is always lowered to
It is the right function to use only if its behavior is what you want. If, however, what you want is the behavior of the default |
Also, please actually check the document of
and also crossed linked to Improving the doc is certainly always welcome but AFAICT there isn't anything missing for this issue so there's no need to keep an issue open for potential generic doc improvement. |
Sorry, I have no idea why I missed that part. Thanks for the clarification. |
New functions `Base.getproperty` and `Base.setproperty!` can be overloaded to change the behavior of `x.p` and `x.p = v`, respectively. This forces inference constant propagation through get/setproperty, since it is very likely this method will yield better information after specializing on the field name (even if `convert` is too big to make us want to inline the generic version and trigger the heuristic normally). closes #16195 (and thus also closes #16226) fix #1974
The last line throws
TypeError: setfield!: expected Float64, got Int64
.The text was updated successfully, but these errors were encountered: