Skip to content

Commit 1f15a50

Browse files
authored
Merge pull request #556 from JuliaParallel/jps/parser-extras
`@spawn`: Support NamedTuple construction and access
2 parents 42e2a32 + e383c39 commit 1f15a50

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

Diff for: src/thunk.jl

+22-4
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,34 @@ function replace_broadcast(fn::Symbol)
362362
return fn
363363
end
364364

365+
to_namedtuple(;kwargs...) = (;kwargs...)
366+
365367
function _par(ex::Expr; lazy=true, recur=true, opts=())
366368
f = nothing
367369
body = nothing
368370
arg1 = nothing
369-
if recur && @capture(ex, f_(allargs__)) || @capture(ex, f_(allargs__) do cargs_ body_ end) || @capture(ex, allargs__->body_) || @capture(ex, arg1_[allargs__])
371+
arg2 = nothing
372+
if recur && @capture(ex, f_(allargs__)) ||
373+
@capture(ex, f_(allargs__) do cargs_ body_ end) ||
374+
@capture(ex, allargs__->body_) ||
375+
@capture(ex, arg1_[allargs__]) ||
376+
@capture(ex, arg1_.arg2_) ||
377+
@capture(ex, (;allargs__))
370378
f = replace_broadcast(f)
371379
if arg1 !== nothing
372-
# Indexing (A[2,3])
373-
f = Base.getindex
374-
pushfirst!(allargs, arg1)
380+
if arg2 !== nothing
381+
# Getproperty (A.B)
382+
f = Base.getproperty
383+
allargs = Any[arg1, QuoteNode(arg2)]
384+
else
385+
# Indexing (A[2,3])
386+
f = Base.getindex
387+
pushfirst!(allargs, arg1)
388+
end
389+
end
390+
if f === nothing && body === nothing
391+
# NamedTuple ((;a=1, b=2))
392+
f = to_namedtuple
375393
end
376394
args = filter(arg->!Meta.isexpr(arg, :parameters), allargs)
377395
kwargs = filter(arg->Meta.isexpr(arg, :parameters), allargs)

Diff for: test/thunk.jl

+26-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ end
125125
@test t isa Dagger.DTask
126126
@test fetch(t) == A[1, 2]
127127

128-
B = Dagger.@spawn rand(4, 4)
128+
t = @spawn A[2]
129+
@test t isa Dagger.DTask
130+
@test fetch(t) == A[2]
131+
132+
B = @spawn rand(4, 4)
129133
t = @spawn B[1, 2]
130134
@test t isa Dagger.DTask
131135
@test fetch(t) == fetch(B)[1, 2]
@@ -135,6 +139,27 @@ end
135139
@test t isa Dagger.DTask
136140
@test fetch(t) == 42
137141
end
142+
@testset "NamedTuple" begin
143+
t = @spawn (;a=1, b=2)
144+
@test t isa Dagger.DTask
145+
@test fetch(t) == (;a=1, b=2)
146+
147+
t = @spawn (;)
148+
@test t isa Dagger.DTask
149+
@test fetch(t) == (;)
150+
end
151+
@testset "getproperty" begin
152+
nt = (;a=1, b=2)
153+
154+
t = @spawn nt.b
155+
@test t isa Dagger.DTask
156+
@test fetch(t) == nt.b
157+
158+
nt2 = @spawn (;a=1, b=3)
159+
t = @spawn nt2.b
160+
@test t isa Dagger.DTask
161+
@test fetch(t) == fetch(nt2).b
162+
end
138163
@testset "invalid expression" begin
139164
@test_throws LoadError eval(:(@spawn 1))
140165
@test_throws LoadError eval(:(@spawn begin 1 end))

0 commit comments

Comments
 (0)