Skip to content

Commit a31fa90

Browse files
authored
Merge pull request #138 from kmsquire/feature/isdef-function-with-return-types
Allow isdef() to match functions with return types
2 parents 75535ac + 7ae6e14 commit a31fa90

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/utils.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ end
215215

216216

217217
"Test for function definition expressions."
218-
isdef(ex) = ismatch(or_(:(function _(__) _ end),
219-
:(f_(__) = _)),
220-
ex)
218+
isdef(ex) = isshortdef(ex) || longdef1(ex) !== nothing
221219

222220
isshortdef(ex) = (@capture(ex, (fcall_ = body_)) &&
223221
(@capture(gatherwheres(fcall)[1],

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ using Test
66
include("match.jl")
77
include("split.jl")
88
include("destruct.jl")
9+
include("utils.jl")
910

1011
end

test/utils.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using MacroTools: isdef
2+
3+
@testset "utils" begin
4+
ex1 = :(function foo(a) return a; end)
5+
@test isdef(ex1)
6+
ex2 = :(function bar(a)::Int return 1; end)
7+
@test isdef(ex2)
8+
ex3 = :(function foo(a::T) where T return a; end)
9+
@test isdef(ex3)
10+
ex4 = :(function bar(a::T)::Int where T return 1; end)
11+
@test isdef(ex4)
12+
ex5 = :(function bar(a::S, b::T)::Union{S,T} where {S,T} if rand() < 0.5 return a; end; return b; end)
13+
@test isdef(ex5)
14+
15+
ex6 = :(f(a) = a)
16+
@test isdef(ex6)
17+
ex7 = :(f(a)::Int == 1)
18+
@test isdef(ex7)
19+
ex8 = :(f(a::T) where T = a)
20+
@test isdef(ex8)
21+
ex9 = :(f(a::T)::Int where T = 1)
22+
@test isdef(ex9)
23+
ex10 = :(f(a::S, b::T)::Union{S,T} where {S,T} = rand() < 0.5 ? a : b)
24+
@test isdef(ex10)
25+
end

0 commit comments

Comments
 (0)