-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Inference failure for Ref{Int}? #38133
Comments
The issue is that
So certainly a gotcha, I wouldn't classify it as an inference failure. |
Thanks @vchuravy , good to know! I'll use RefValue. |
Yes, generally better to use --- a/base/refpointer.jl
+++ b/base/refpointer.jl
@@ -178,7 +178,7 @@ unsafe_convert(::Type{Ptr{T}}, r::Ptr{NTuple{N,T}}) where {N,T} =
###
-getindex(b::RefArray) = b.x[b.i]
+getindex(b::RefArray{T}) where {T} = b.x[b.i]::T
setindex!(b::RefArray, x) = (b.x[b.i] = x; b)
### Note that |
Parametrization feels like an awkward hack as I don't need the struct with any other type than the one declared, and with multiple |
Maybe make the struct |
Any cost to a mutable struct with all constant fields as compared to an immutable struct? |
They have somewhat different semantics: the identity of two identical mutable structs must remain separate (and more importantly coherent), so it's harder for the compiler to completely eliminate allocations. The most straightforward and default way to ensure that is to actually allocate objects with location-based identity. mutable struct M
const f::Int
end
struct I
f::Int
end # basic identity
julia> I(1) === I(1)
true
julia> M(1) === M(1)
false
# more complex example for I
julia> a = I(1)
I(1)
julia> b = I(1)
I(1)
julia> c = a
I(1)
julia> a === b
true
julia> a === c
true
julia> b === c
true
# more complex example for M
julia> a = M(1)
M(1)
julia> b = M(1)
M(1)
julia> c = a
M(1)
julia> a === b
false
julia> a === c
true
julia> b === c
false The language needs to ensure that for type |
I thought this would infer the return type as
Int
, but it only managesAny
. Am I doing something wrong, or is this expected? Is there a situation where the dereference can return something other thanInt
?The text was updated successfully, but these errors were encountered: