Skip to content

Commit

Permalink
Simplify code with @q begin $__source__
Browse files Browse the repository at this point in the history
  • Loading branch information
cstjean committed Dec 11, 2023
1 parent 93b3add commit df2eb2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/QuickTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,13 @@ macro qfunctor(fdef0)
di[:whereparams] = tuple(Ts..., get(di, :whereparams, ())...)
end
di[:body] =
Expr(:block, # written as Expr(:block, __source__) to have correct line numbers
__source__,
# I wish I could have used @unpack_Foo, but it seems we can't define a macro and use
# it in the same top-level block.
:($(Expr(:tuple, Expr(:parameters, all_args...))) = __self__),#namedtuple destructuring
di[:body])
@q begin
$__source__
# I wish I could have used @unpack_Foo, but it seems we can't define a macro and use
# it in the same top-level block.
$(Expr(:tuple, Expr(:parameters, all_args...))) = __self__ # namedtuple destructuring
$(di[:body])
end
esc(quote
$QuickTypes.@qstruct $type_def <: $parenttype
$(combinedef(di))
Expand Down
7 changes: 5 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ using QuickTypes: construct, roottypeof, fieldsof, type_parameters, roottype,
using Test
using ConstructionBase: setproperties

""" Test that the @which returns the source of the @qstruct macro call, instead of
None/QuickTypes.jl """
is_defined_in_runtest(method::Method) = occursin("runtests.jl", string(method.file))

abstract type Vehicle end

@qstruct Car{T<:Number, U}(size::T, nwheels::Int=4; manufacturer::U=nothing,
Expand Down Expand Up @@ -134,8 +138,7 @@ convert_f(foo) = convert(foo.a, 10)
@qfunctor function Action(a; kw=100)(x)
return a + x + kw
end
# Test that the
@test occursin("runtests.jl", string(which(Action(1), Tuple{Int}).file))
@test is_defined_in_runtest(which(Action(1), Tuple{Int}))

@test Action(2)(10) == 112

Expand Down

0 comments on commit df2eb2c

Please sign in to comment.