Julia: no Base.tail for NamedTuple

Created on 9 Oct 2018  路  4Comments  路  Source: JuliaLang/julia

julia> Base.tail((a = 1, b = 2))
ERROR: MethodError: no method matching tail(::NamedTuple{(:a, :b),Tuple{Int64,Int64}})
Closest candidates are:
  tail(::Tuple) at essentials.jl:173
Stacktrace:
 [1] top-level scope at none:0

julia> VERSION
v"1.1.0-DEV.431"

Is this intentional, or is it just waiting for me to make a PR?

Most helpful comment

Named tuples are ordered though, so returning a new named tuple with the first element dropped seems to make sense.

All 4 comments

I suspect it's just an oversight; @JeffBezanson?

Yes this can be added. tail is not exported, but also the named tuples API is deliberately minimal since adding new, otherwise undefined methods is non-breaking. That gives us time to decide what should be defined and how it should work.

Reflecting on this, think I agree with the wait & see approach, since I am not sure what semantics I would find useful. Originally I was expecting

Base.tail((a = 1, b = 2, c = 3)) == (b = 2, c = 3)

but then it turned out I need to work with the values anyway, which is a ::Tuple and I can just use Base.tail. Also, first returns just the first value, so

(first(x), Base.tail(x)...) == x

would not hold either.

The alternative is making

Base.tail(nt::NamedTuple) = Base.tail(values(nt))

which seems kind of contrived too.

Named tuples are ordered though, so returning a new named tuple with the first element dropped seems to make sense.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

m-j-w picture m-j-w  路  3Comments

manor picture manor  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

sbromberger picture sbromberger  路  3Comments

musm picture musm  路  3Comments