Skip to content

Commit bf2d515

Browse files
authored
Merge pull request #255 from JuliaGPU/tb/allowscalar_function
Add a function for temporarily allowing scalar indexing.
2 parents 5233b45 + 1788d90 commit bf2d515

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/host/indexing.jl

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ end
4949
"""
5050
@allowscalar ex...
5151
@disallowscalar ex...
52+
allowscalar(::Function, ...)
5253
5354
Temporarily allow or disallow scalar iteration.
5455
@@ -77,6 +78,15 @@ macro disallowscalar(ex)
7778
end
7879
end
7980

81+
@doc (@doc @allowscalar) ->
82+
function allowscalar(f::Base.Callable, allow::Bool=true, warn::Bool=false)
83+
prev = scalar_allowed[]
84+
allowscalar(allow, warn)
85+
ret = f()
86+
scalar_allowed[] = prev
87+
ret
88+
end
89+
8090

8191
# basic indexing
8292

test/testsuite/indexing.jl

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ function test_indexing(AT)
2424
@test x[1] == 3
2525
end
2626

27+
allowscalar() do
28+
x[1] = 4
29+
@test x[1] == 4
30+
end
31+
2732
@test_throws ErrorException x[1]
2833
@test_throws ErrorException x[1] = 1
2934

0 commit comments

Comments
 (0)