Ref -> Array{Bool,0} in NCDataset to make types concrete#300
Ref -> Array{Bool,0} in NCDataset to make types concrete#300Alexander-Barth merged 3 commits intoJuliaGeo:mainfrom
Conversation
|
Being compatible with Reactant.jl is indeed something I would like to see. :-) An alternative could be zero-dimensional arrays: julia> struct Foo; f::Array{Bool,0}; end
julia> fun(p) = (p.f,p.f[])
fun (generic function with 1 method)
julia> foo = Foo(fill(true))
# type-stable (unlike Ref)
julia> @code_warntype(fun(foo))
MethodInstance for fun(::Foo)
from fun(p) @ Main REPL[25]:1
Arguments
#self#::Core.Const(Main.fun)
p::Foo
Body::Tuple{Array{Bool, 0}, Bool}
1 ─ %1 = Base.getproperty(p, :f)::Array{Bool, 0}
│ %2 = Base.getproperty(p, :f)::Array{Bool, 0}
│ %3 = Base.getindex(%2)::Bool
│ %4 = Core.tuple(%1, %3)::Tuple{Array{Bool, 0}, Bool}
└── return %4
julia> isconcretetype(foo)
false
julia> r = Ref(true)
Base.RefValue{Bool}(true)
julia> isconcretetype(r)
false |
|
Thanks for the quick response!
It is, you just made a small mistake there: julia> isconcretetype(Base.RefValue{Bool})
true
julia> isconcretetype(Ref{Bool})
false
Yeah, that's a bit odd in my opinion, because why is the abstract type public API, but the concrete type isn't? We could also just have a type parameter for the ref value, that would work as well, I think, without using mutable struct NCDataset{TDS,Tmaskingvalue, RefV} <: AbstractNCDataset where TDS <: Union{AbstractNCDataset,Nothing}
# parent_dataset is nothing for the root dataset
parentdataset::TDS
ncid::Cint
iswritable::Bool
# true of the NetCDF is in define mode (i.e. metadata can be added, but not data)
# need to be a reference, so that remains syncronised when copied
isdefmode::RefV
# mapping between variables related via the bounds attribute
# It is only used for read-only datasets to improve performance
_boundsmap::Dict{String,String}
maskingvalue::Tmaskingvalue
endBtw as you seem interested about Reactant compatibility in general: so far I am just starting with this for our model (SpeedyWeather.jl). Without this fix you can't have a |
|
Ah, that makes sense: While |
|
Yes, the 0d-array works as well. With Reactant as well, afaik as long as it's concrete it's fine for Reactant. |
|
Thanks a lot, Maximilian! |
In the definition of
NCDatasetRef{Bool}isn't a concrete type, butBase.RefValue{Bool}is.The definition with
Ref{Bool}is causing problems for me when using NCDatasets as part of our models with Reactant.jl, and changing it toBase.RefValue{Bool}fixes that.