-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add basetype to strip type parameters from type #46213
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) == Array And 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.wrapper
every single time is a bit tedious. Following what I read in the different issues which have been opened, I created thebasetype
function which does just this.I just didn't know exactly where to put this script: I saw that
typename
was already defined in essentials, so that's why I put it there, but maybe it needs to go elsewhere.