Skip to content

Instantly share code, notes, and snippets.

@latticetower
Created August 10, 2016 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latticetower/004c3e0d30dc761d0e6b824cf19278e3 to your computer and use it in GitHub Desktop.
Save latticetower/004c3e0d30dc761d0e6b824cf19278e3 to your computer and use it in GitHub Desktop.
type C0{T}
super :: T#base obj
x :: Int#subtype fields
function C0(x::Int, y::Int)
new(T(x), y) # вызвали конструктор базового типа, если надо, ну и добавили сложную логику
end
end
function convert{T}(::Type{C0{T}}, x::Int, y::Int)
C0{T}(x, y)
end
a=C0{Int}(1,2) #но в качестве параметра надо явно указывать базовый тип
typeof(a.super)
type Rep
x::Int
y::Int
Rep(x::Int)=new(x, x)
end
#какой-то псевдо-базовый класс
b=C0{Rep}(1,2)
b.super
# если мы хотим вызывать "дочерний" тип в функциях, которые определены для базового, можно вот это доопределить
function convert{T}(::Type{T}, child :: C0{T})
child.super
end
convert(Rep, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment