abstraction question

206 views
Skip to first unread message

Jameson Nash

unread,
Feb 10, 2014, 11:26:33 PM2/10/14
to julia...@googlegroups.com
I can't figure this out, so I'm hoping someone will be able to help
suggest a better way to parametrize this code, or solve my dilemma.
I've made a somewhat complicate type hierarchy, and now I need to
extract some information from it. The problem is, given z (or an
instance thereof), I can't seem to extract S:


julia> abstract A{S}

julia> type B{T} <: A{S} end

julia> typealias C{T<:A} Ptr{T}
Ptr{T<:A{T}}

julia> z = Ptr{B{Int}}
Ptr{B{Int64}}

julia> # how to go from z -> Int here

Stefan Karpinski

unread,
Feb 10, 2014, 11:28:28 PM2/10/14
to Julia Users
Did you mean for S to be T or vice versa?

Isaiah Norton

unread,
Feb 10, 2014, 11:35:34 PM2/10/14
to julia...@googlegroups.com
z.parameters[1].parameters[1]

?


On Mon, Feb 10, 2014 at 11:26 PM, Jameson Nash <vtj...@gmail.com> wrote:

Jameson Nash

unread,
Feb 11, 2014, 12:09:49 AM2/11/14
to julia...@googlegroups.com
> Did you mean for S to be T or vice versa?
Yes. I was trying to disambiguate them, but failed.

> z.parameters[1].parameters[1]
Tried that, but it caused other problems:
https://github.com/JuliaLang/julia/issues/5752

Tim Holy

unread,
Feb 11, 2014, 10:45:21 AM2/11/14
to julia...@googlegroups.com
Does this do it?

ptype{T}(::Type{Ptr{T}}) = T
btype{T}(b::Type{B{T}}) = T
btype(ptype(z))

(One way or another, that is one torturous type nest!)

--Tim

Jameson Nash

unread,
Feb 14, 2014, 8:12:12 AM2/14/14
to julia...@googlegroups.com
no. that misses the usefulness of abstract types. Here's the answer:

abstract A{S}
type B{T} <: A{T} end
typealias C{T<:A} Ptr{T}
z = Ptr{B{Int}}

import Base.eltype
eltype{T}(::Type{A{T}}) = T
eltype{T<:A}(::Type{T}) = eltype(super(T))
eltype{T}(::Type{C{T}}) = eltype(T)

julia> eltype(z)
Int64
Reply all
Reply to author
Forward
0 new messages