Skip to content

Commit e383c39

Browse files
committed
parse: Support NamedTuple construction
1 parent 11b3f8e commit e383c39

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/thunk.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ 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
@@ -371,7 +373,8 @@ function _par(ex::Expr; lazy=true, recur=true, opts=())
371373
@capture(ex, f_(allargs__) do cargs_ body_ end) ||
372374
@capture(ex, allargs__->body_) ||
373375
@capture(ex, arg1_[allargs__]) ||
374-
@capture(ex, arg1_.arg2_)
376+
@capture(ex, arg1_.arg2_) ||
377+
@capture(ex, (;allargs__))
375378
f = replace_broadcast(f)
376379
if arg1 !== nothing
377380
if arg2 !== nothing
@@ -384,6 +387,10 @@ function _par(ex::Expr; lazy=true, recur=true, opts=())
384387
pushfirst!(allargs, arg1)
385388
end
386389
end
390+
if f === nothing && body === nothing
391+
# NamedTuple ((;a=1, b=2))
392+
f = to_namedtuple
393+
end
387394
args = filter(arg->!Meta.isexpr(arg, :parameters), allargs)
388395
kwargs = filter(arg->Meta.isexpr(arg, :parameters), allargs)
389396
if !isempty(kwargs)

test/thunk.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ end
129129
@test t isa Dagger.DTask
130130
@test fetch(t) == A[2]
131131

132-
B = Dagger.@spawn rand(4, 4)
132+
B = @spawn rand(4, 4)
133133
t = @spawn B[1, 2]
134134
@test t isa Dagger.DTask
135135
@test fetch(t) == fetch(B)[1, 2]
@@ -139,12 +139,26 @@ end
139139
@test t isa Dagger.DTask
140140
@test fetch(t) == 42
141141
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
142151
@testset "getproperty" begin
143152
nt = (;a=1, b=2)
144153

145154
t = @spawn nt.b
146155
@test t isa Dagger.DTask
147156
@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
148162
end
149163
@testset "invalid expression" begin
150164
@test_throws LoadError eval(:(@spawn 1))

0 commit comments

Comments
 (0)