Skip to content

Commit

Permalink
add rest for NamedTuple and fix some destructuring cases (#38214)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Oct 29, 2020
1 parent 905ebec commit 78ade6c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ end # if Base

length(t::NamedTuple) = nfields(t)
iterate(t::NamedTuple, iter=1) = iter > nfields(t) ? nothing : (getfield(t, iter), iter + 1)
rest(t::NamedTuple) = t
@inline rest(t::NamedTuple{names}, i::Int) where {names} = NamedTuple{rest(names,i)}(t)
firstindex(t::NamedTuple) = 1
lastindex(t::NamedTuple) = nfields(t)
getindex(t::NamedTuple, i::Int) = getfield(t, i)
Expand Down
3 changes: 2 additions & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,8 @@
((null? r) #f)
((vararg? (car r)) (null? (cdr r)))
(else (sides-match? (cdr l) (cdr r)))))
(if (and (pair? x) (pair? lhss) (eq? (car x) 'tuple)
(if (and (pair? x) (pair? lhss) (eq? (car x) 'tuple) (not (any assignment? (cdr x)))
(not (has-parameters? (cdr x)))
(sides-match? lhss (cdr x)))
;; (a, b, ...) = (x, y, ...)
(expand-forms
Expand Down
18 changes: 18 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2582,4 +2582,22 @@ end
cdr((a, d...)) = d
@test car(1:3) == 1
@test cdr(1:3) == [2, 3]

@test begin a, b = (;c = 3, d = 4) end === (c = 3, d = 4)
@test begin a, b, c = (x = "", y = 2.0, z = 1) end === (x = "", y = 2.0, z = 1)
a, b, c = (x = "", y = 2.0, z = 1)
@test a === ""
@test b === 2.0
@test c === 1
@test begin a, b... = (x = "", y = 2.0, z = 1) end === (x = "", y = 2.0, z = 1)
a, b... = (x = "", y = 2.0, z = 1)
@test b === (y = 2.0, z = 1)
let t = (x = "", y = 1, z = 3.0)
_, a, b = t
@test a === 1
@test b === 3.0
a, b... = t
@test a === ""
@test b === (y = 1, z = 3.0)
end
end

0 comments on commit 78ade6c

Please sign in to comment.