Check whether the type of an object is from a package not loaded

I have a package A in which I’d like to have a functionality foo which works on AbstractArray but does something different on LazyArrays.ApplyArray.
twist: I would like not to have LazyArrays loaded in A.

Is this possible?

a bit of a longer explanation

CSV, in its threaded mode, generates LazyArrays.ApplyArray, this causes a bit of an issue with something we’re doing in ScientificTypes.jl (issue here for context but otherwise irrelevant I would think).

A hacky workaround is to check the propertynames of the object as it turns out ApplyArray have a :f and :args field, but that’s really not great and I can’t robustly assume that’s a one-to-one signature.

I can’t do something like typeof(vec) <: LazyArrays.ApplyArray because that requires loading LazyArrays in ScientificTypes which I’d rather not do to keep the package as light as possible.

Thanks!

Are you aware of https://github.com/MikeInnes/Requires.jl?

2 Likes

Yes, thanks. I’m looking for a package-free way of doing this though (or at least something that doesn’t double the package loading time).

I think I found a way though, the key thing is that typeof(object) does return LazyArrays.ApplyArray but I can’t directly compare that because LazyArrays is not loaded. However I can compare it as a String…

So for someone who may be looking for a similar solution, here’s something that works and is better than checking the propertynames:

julia> Threads.nthreads()
2

julia> using RDatasets; boston = dataset("MASS", "Boston");

julia> typeof(boston.Chas)
LazyArrays.ApplyArray{Int64,1,typeof(vcat),Tuple{Array{Int64,1},Array{Int64,1}}}

julia> "$(typeof(boston.Chas))"
"LazyArrays.ApplyArray{Int64,1,typeof(vcat),Tuple{Array{Int64,1},Array{Int64,1}}}"

julia> startswith("$(typeof(boston.Chas))", "LazyArrays.ApplyArray")
true

julia> typeof(boston.Chas) <: LazyArrays.ApplyArray
ERROR: UndefVarError: LazyArrays not defined
Stacktrace:
 [1] top-level scope at none:0

PS: for completeness it should also check whether it’s just ApplyArray (in case LazyArrays is loaded in the env)

Happy to learn of other ways though or to consider why using the above hack could be problematic…