JuliaStats / GLM.jl

Generalized linear models in Julia

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Link doesn't behave like an abstract type

biona001 opened this issue · comments

Given a vector of links, I expected their type of be <: Vector{Link}, but this is not generally true:

julia> using GLM
julia> v = [LogLink(), LogLink()]

2-element Vector{LogLink}:
 LogLink()
 LogLink()

julia> typeof(v) <: Vector{Link}
false

julia> v = [IdentityLink(), LogLink()]
2-element Vector{Link}:
 IdentityLink()
 LogLink()

julia> typeof(v) <: Vector{Link}
true

Is this expected?

This is due to type covariance in Julia, it's not specific to GLM. You need <:Link:

julia> using GLM

julia> v = [LogLink(), LogLink()]
2-element Vector{LogLink}:
 LogLink()
 LogLink()

julia> typeof(v) <: Vector{<:Link}
true

You get the same phenomenon with e.g.:

julia> typeof([1, 1]) <: Vector{Number}
false

julia> typeof([1, 1]) <: Vector{<:Number}
true