Skip to content

Commit 8f7cca2

Browse files
committed
improve tests of :thisfunction
1 parent 56632e5 commit 8f7cca2

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

test/syntax.jl

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4352,39 +4352,58 @@ let f = NoSpecClosure.K(1)
43524352
@test typeof(f).parameters == Core.svec()
43534353
end
43544354

4355-
# Expr(:thisfunction)
4356-
# regular functions can use Expr(:thisfunction) to refer to the function itself
4357-
@eval regular_func() = $(Expr(:thisfunction))
4358-
@test regular_func() === regular_func
4355+
@testset "Expr(:thisfunction)" begin
4356+
# regular functions can use Expr(:thisfunction) to refer to the function itself
4357+
@eval regular_func() = $(Expr(:thisfunction))
4358+
@test regular_func() === regular_func
4359+
4360+
# This also works in callable structs, which refers to the instance
4361+
struct CallableStruct
4362+
value::Int
4363+
end
4364+
@eval (obj::CallableStruct)() = $(Expr(:thisfunction))
4365+
@eval (obj::CallableStruct)(x) = $(Expr(:thisfunction)).value + x
43594366

4360-
# This also works in callable structs, which refers to the instance
4361-
struct CallableStruct
4362-
value::Int
4363-
end
4364-
@eval (obj::CallableStruct)() = $(Expr(:thisfunction))
4365-
@eval (obj::CallableStruct)(x) = $(Expr(:thisfunction)).value + x
4367+
let cs = CallableStruct(42)
4368+
@test cs() === cs
4369+
@test cs(10) === 52
4370+
end
43664371

4367-
let cs = CallableStruct(42)
4368-
@test cs() === cs
4369-
@test cs(10) === 52
4370-
end
4372+
struct RecursiveCallableStruct; end
4373+
@eval (::RecursiveCallableStruct)(n) = n <= 1 ? n : $(Expr(:thisfunction))(n-1) + $(Expr(:thisfunction))(n-2)
43714374

4372-
struct RecursiveCallableStruct; end
4373-
@eval (::RecursiveCallableStruct)(n) = n <= 1 ? n : $(Expr(:thisfunction))(n-1) + $(Expr(:thisfunction))(n-2)
4375+
@test RecursiveCallableStruct()(10) === 55
43744376

4375-
@test RecursiveCallableStruct()(10) === 55
4377+
# In closures, var"#self#" should refer to the enclosing function,
4378+
# NOT the enclosing struct instance
4379+
struct CallableStruct2; end
4380+
@eval function (obj::CallableStruct2)()
4381+
function inner_func()
4382+
$(Expr(:thisfunction))
4383+
end
4384+
inner_func
4385+
end
43764386

4377-
# In closures, var"#self#" should refer to the enclosing function,
4378-
# NOT the enclosing struct instance
4379-
struct CallableStruct2; end
4380-
@eval function (obj::CallableStruct2)()
4381-
function inner_func()
4382-
$(Expr(:thisfunction))
4387+
let cs = CallableStruct2()
4388+
@test cs()() === cs()
4389+
@test cs()() !== cs
43834390
end
4384-
inner_func
4385-
end
43864391

4387-
let cs = CallableStruct2()
4388-
@test cs()() === cs()
4389-
@test cs()() !== cs
4392+
# Keywords
4393+
let
4394+
@eval f2(; n=1) = n <= 1 ? n : n * $(Expr(:thisfunction))(; n=n-1)
4395+
result = f2(n=5)
4396+
@test result == 120
4397+
end
4398+
4399+
# Struct constructor with thisfunction
4400+
let
4401+
@eval struct Cols{T<:Tuple}
4402+
cols::T
4403+
operator
4404+
Cols(args...; operator=union) = (new{typeof(args)}(args, operator); string($(Expr(:thisfunction))))
4405+
end
4406+
result = Cols(1, 2, 3)
4407+
@test result == "Cols"
4408+
end
43904409
end

0 commit comments

Comments
 (0)