Skip to content

Commit

Permalink
@with_kw only called if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jkosata committed Aug 23, 2023
1 parent e0856c3 commit 6fa444f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/AbstractFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ macro with_fields(namedef, fields...)
macrodef = quote
macro $(macroname)(typedef)
typedef.args[2] = :($(typedef.args[2]) <: $$macroname)
total_def = $add_field!(typedef, $fields...)

if $_has_kwdefs(typedef)
# evaluate the with_kw function (not macro), interpolating from this module
esc($Parameters.with_kw(total_def, @__MODULE__))
else
esc(total_def)
end

# evaluate the with_kw function, interpolating from this module
esc($Parameters.with_kw($add_field!(typedef, $fields...), @__MODULE__))
end
end
esc(:($absdef, $macrodef))
Expand All @@ -76,6 +82,6 @@ _fieldnames(arb) = nothing
""" Return the list of elements in array which occur more than once. """
_duplicates(a) = filter( el -> length(findall(==(el), a)) > 1, unique(a) )

_has_kwdefs(fieldsdef) = any( (f isa Expr && f.head == :(=) for f in fieldsdef) )
_has_kwdefs(typedef::Expr) = any( (f isa Expr && f.head == :(=) for f in typedef.args[3].args) )

end

0 comments on commit 6fa444f

Please sign in to comment.