Skip to content

No automatic type promotion for parametric structs #41241

@lungben

Description

@lungben

See discussion in https://discourse.julialang.org/t/no-automatic-type-promotion-for-parametric-structs/62664/1

When using different (but convertible) types in a constructor, for "standard" structs the types get converted automatically, e.g.

struct Struct1
    x:: Float64
    y
end

s1b = Struct1(5, 1) # output: Struct1b(5.0, 1)

However, this is not done if the struct has a type parameter (even if this parameter is for a different field), instead I get a MethodError:

struct Struct2{T}
    x:: Float64
    y:: T
end

s2b = Struct2(5, 1) # MethodError, I would expect Struct2{Int64}(5.0, 1)

s2c = Struct2(5.0, 1) # this works, output: Struct2{Int64}(5.0, 1)

This behavior is confusing for me, I would expect that type promotion is also done for parametric structs. Or is there a good reason for it which I am not aware of?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions