Skip to content

Defining a Foo{T} constructor for Foo{T, S <: T} #39280

@CameronBieganek

Description

@CameronBieganek

The manual doesn't provide a definition for what it means to define a constructor for a parametric type where the number of type parameters accepted by the constructor is fewer than the number of type parameters for the type. In simple cases, it works as expected, but for more complicated cases, the lack of a formal semantics leads to mass confusion, as demonstrated by this Discourse thread.

From that thread, we came up with the following MWE.

This works:

julia> struct Bar{T, S} end

julia> Bar{T}() where {T} = T

julia> Bar{Integer}()
Integer

But this does not:

julia> struct Foo{T, S <: T} end

julia> Foo{T}() where {T} = T

julia> Foo{Integer}()
ERROR: UndefVarError: T not defined
Stacktrace:
 [1] Foo{Integer,S} where S<:Integer() at ./REPL[2]:1
 [2] top-level scope at REPL[3]:1

Without a definition of what it means to define Foo{T}() where {T} = # ... it's difficult to know if this is a bug or if this is expected behavior. And if it is the expected behavior, it would be nice to know what the semantic rule is that explains this behavior (and it would be nice to have that explanation in the manual).

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviordocsThis change adds or pertains to documentationtypes 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