Skip to content

Commit d321c2e

Browse files
authored
Merge pull request #21 from bgroenks96/bugfix/callable-struct-signature
Allow callable structs in argument_names
2 parents b374c69 + 2aec712 commit d321c2e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/method.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,15 @@ function signature(orig_sig::Type{<:Tuple}; extra_hygiene=false)
9292
return def
9393
end
9494

95-
96-
9795
function slot_names(m::Method)
9896
ci = Base.uncompressed_ast(m)
9997
return ci.slotnames
10098
end
10199

102100
function argument_names(m::Method)
103101
slot_syms = slot_names(m)
104-
@assert slot_syms[1] === Symbol("#self#")
105-
arg_names = slot_syms[2:m.nargs] # nargs includes 1 for `#self#`
102+
# nargs includes 1 for `#self#` or the function object name;
103+
arg_names = slot_syms[2:m.nargs]
106104
return arg_names
107105
end
108106

test/method.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ function only_method(f, typ=Tuple{Vararg{Any}})
4242
return first(ms)
4343
end
4444

45+
struct TestCallableStruct end
46+
(self::TestCallableStruct)(x) = 2x
47+
(self::TestCallableStruct)(x::T,y::R) where {T,R} = 2x + y
48+
4549
@testset "method.jl: signature" begin
4650
@testset "Basics" begin
4751
@test_signature basic1(x) = 2x
@@ -112,6 +116,16 @@ end
112116
@test_signature ((::T) where T) -> 0 # Anonymous parameter
113117
end
114118

119+
@testset "callable structs" begin
120+
ms = collect(methods(TestCallableStruct()))
121+
sig1 = signature(first(filter(m -> m.nargs == 2, ms)))
122+
@test sig1[:name] == :TestCallableStruct
123+
@test sig1[:args] == [:x]
124+
sig2 = signature(first(filter(m -> m.nargs == 3, ms)))
125+
@test sig2[:name] == :TestCallableStruct
126+
@test sig2[:args] == Expr[:(x::T),:(y::R)]
127+
end
128+
115129
@testset "vararg" begin
116130
@test_signature f17(xs::Vararg{Any, N} where N) = 2
117131

0 commit comments

Comments
 (0)