Skip to content

Struct field cannot have same name as type variable unless the type of the field is also the type variable #46668

Closed
@llbit

Description

@llbit

I noticed that a struct like struct S{T} T::String end does not compile. Therefore I expected that struct S{T} T::T end would likewise not compile because both of them have a field with the same name as a type variable (the error message seems to imply there is a name collision causing the problem). However, the latter case was accepted by the Julia compiler.

This seems like a compiler error or an oversight in Julia semantics. Also the error message could be greatly improved to explain what the compiler thinks is wrong in the context of the surface syntax.

julia> VERSION
v"1.8.0"

julia> struct S{T} T::String end
ERROR: syntax: function argument and static parameter names must be distinct around REPL[2]:1
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> struct S{T} T::T end

julia>

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