Add basetype to strip type parameters from type#46213
Add basetype to strip type parameters from type#46213GVigne wants to merge 1 commit intoJuliaLang:masterfrom
Conversation
vtjnash
left a comment
There was a problem hiding this comment.
This definition is only partly correct for concrete types, which makes in incorrect in general (#35543 (comment))
|
I think these are some examples of what @vtjnash is talking about. This should work: basetype(Vector) == ArrayAnd perhaps some of these should also work: basetype(Union{Vector{Int64}, Vector{Float64}) == Array
basetype(Union{Vector, Matrix}) == Array
basetype(Union{Vector, Matrix, SparseMatrixCSC{Int}}) == Union{Array, SparseMatrixCSC}
basetype(Union{Int, Float64}) == Union{Int, Float64}Currently, they all throw. |
|
Thanks for the examples! Here is what I came up with to solve them: Can you think of other examples where this would fail or give unwanted results? |
|
The PRs "inspired by" this PR are likely to be wrong, hardcoding incorrect assumptions about the subtyping system into the code, which is why I don't think this PR or the issue that spawned it is a good idea: JuliaArrays/StaticArrays.jl#1064 (comment) @LilithHafner we already have |
|
I understand what you are saying. Encouraging package developers to use a fragile function can only lead to bad surprises. I am working on a GPU version of the DFTK package. The goal is to have a unique base of code, and not a duplicate code for GPU. We have identified an array which should always be on GPU if we are doing GPU computations, and so instead of having a generic variable telling us if we are on GPU or CPU, we would like to simply be able to look at the type of this array.
|
The name of this function is
The name of this function is probably |
|
I am aware of I will investigate on the two functions you gave, but they didn't immediately work so I don't know if they will do the trick. |
|
We figured out a much simpler way to do what we wanted. We simply added the array type in one of our structure as a parametric type, so it's very simple to just get the "base type" of the arrays used for computations. |
This is a followup of this issue.
Recently I have had the need to get the "base type" of a type, ie to strip type parameters from type. It seems that I am far from being the first to have such a problem, and having to call
T.name.wrapperevery single time is a bit tedious. Following what I read in the different issues which have been opened, I created thebasetypefunction which does just this.I just didn't know exactly where to put this script: I saw that
typenamewas already defined in essentials, so that's why I put it there, but maybe it needs to go elsewhere.