Skip to content

Provide better constructors for type with tag-parameter #17186

@mauro3

Description

@mauro3

The constructor is missing:

julia> type A{T,S}
       a::S
       end

julia> A{Int}(5)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type A{Int64,S}
This may have arisen from a call to the constructor A{Int64,S}(...),
since type constructors fall back to convert methods.
 in A{Int64,S}(::Int64) at ./sysimg.jl:63
 in eval(::Module, ::Any) at ./boot.jl:234
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:46

When the tag-parameter is in the first position (or also multiple), I think it is unambiguous that I mean to call this constructor:

julia> (::Type{A{T}}){T,S}(a::S) = A{T,S}(a)

julia> A{Int16}(5)
A{Int16,Int64}(5)

Would it be possible to automatically provide it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:loweringSyntax lowering (compiler front end, 2nd stage)types and dispatchTypes, subtyping and method dispatch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions