Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covariance support for @NamedTuple #43513

Closed
wants to merge 3 commits into from
Closed

Covariance support for @NamedTuple #43513

wants to merge 3 commits into from

Conversation

o314
Copy link
Contributor

@o314 o314 commented Dec 21, 2021

With @NamedTuple, one can code

using Test
@test (a=1,b=true) isa NamedTuple{(:a,:b),Tuple{Int,Bool}}

by shortening it with

@test (a=1,b=true) isa @NamedTuple{a::Int, b::Bool}

but this fails with, for example, a nullable type

@test_broken (a=1,b=true) isa @NamedTuple{a::Int, b::Union{Bool,Nothing}}
# logical since it expands to :
@test_broken (a=1,b=true) isa NamedTuple{(:a,:b),Tuple{Int,Union{Bool,Nothing}}}
# namedtuple covariance is not handled. this is what we are looking for
@test (a=1,b=true) isa NamedTuple{(:a,:b),<:Tuple{Int,Union{Bool,Nothing}}}

This PR propose to make it work, by adding a <: recognition

@test (a=1,b=true) isa @NamedTuple{<:(a::Int, b::Union{Bool,Nothing})}

nota i do not code the @NamedTuple <: begin ... end form since i consider it less natural.

@JeffBezanson
Copy link
Member

I see the need for the feature, but I don't like the notation <:( ... ). In fact you can do this currently with e.g. @NamedTuple{a::T} where T<:Integer, so we may not need new syntax.

@o314
Copy link
Contributor Author

o314 commented Jan 5, 2022

May be a bit clumsy for the core lang; true. After reviewing #24990 and #29875 i play with different underscore form to get @NamedTuple{a::(_<:Integer)} or even may be its unary form @NamedTuple{a::(<:Integer)}

  • it parses correctly as of v1.6 "@NamedTuple{a::(_<:Integer)}" |> Meta.parse
  • it is not that much disruptive with the current form implemented :
julia> :(@NamedTuple{a::Int}) |> Meta.show_sexpr
(:macrocall, Symbol("@NamedTuple"), :(#= REPL[22]:1 =#), (:braces, (:(::), :a, :Int)))

julia> :(@NamedTuple{a::(_<:Integer)}) |> Meta.show_sexpr
(:macrocall, Symbol("@NamedTuple"), :(#= REPL[23]:1 =#), (:braces, (:(::), :a, (:<:, :_, :Integer))))
  • it does not shake too much the symbolic payload, associativity around instance, type, typevar otherwise (personal opinion ?)
    like
    :(@NamedTuple{a::_<:Integer}) # bad readability, assoc impedance
    :(@NamedTuple{a::_{<:Integer}}) # typevar syntax hijacking
    :(@NamedTuple{a::{<:Integer}}) # is it good ? better ?

EDIT: add the unary form

@o314
Copy link
Contributor Author

o314 commented Jan 5, 2022

May be, not of those are goods enough now. Simply updating the doc of @NamedTuple to suggest how to covariant it with @NamedTuple{a::T} where T<:Integer may be enough.

@JeffBezanson
Copy link
Member

@NamedTuple{a::(<:Integer)} is good, since we already have a rule that <:X inside curly braces means T plus where T<:X added outside. In fact, you can already write this syntax, but it does the wrong thing, since the <: is "captured" by the Tuple{ } expression. That is arguably a bug that should be fixed!

@o314
Copy link
Contributor Author

o314 commented Jan 12, 2022

OK I Understand. FWIW

julia> mapfoldl(append!, (Core,Meta,Base); init=[]) do m
    [(m,nm) for nm in names(m; all=true) if
        occursin("Tuple", string(nm)) && let a=getfield(m,nm); a isa Type && a <: Tuple end]
end
4-element Vector{Any}:
(Core, :NTuple)
(Core, :Tuple)
(Base, :NTuple)
(Base, :VerTuple)
julia> mapfoldl(append!, (Core,Meta,Base); init=[]) do m
    [(m,nm) for nm in names(m; all=true) if
        occursin("Tuple", string(nm)) && let a=getfield(m,nm); a isa Type && !(a <: Tuple) end]
end
3-element Vector{Any}:
(Core, :NamedTuple)
(Base, Symbol("#@NamedTuple"))
(Base, :AbstractVecOrTuple)

lending one day covariance to tuplesomething may not be so contagious.


One or two examples in doc could be helpful and enough .
No pb to code them

@o314 o314 closed this Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants