Skip to content

Element type of Diagonal{T} is T even when typeof(zero(T)) != T #27494

@tkoolen

Description

@tkoolen

From https://discourse.julialang.org/t/help-creating-a-jump-variable-array/11515/8?u=tkoolen. There are cases where typeof(zero(T)) != T, so what should the eltype of Diagonal{T} for such T?

One option would be to define

Base.eltype(::Type{Diagonal{T}}) where {T} = promote_type(T, typeof(zero(T)))

but this could lead to bugs where the T in Diagonal{T} is assumed to be the element type. Another option is to make it so that the Diagonal constructor converts the input vector to e.g. Vector{promote_type(T, typeof(zero(T)))} when necessary.

Maybe erroring in the constructor when typeof(zero(T)) != T could be another option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions