-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dirty macro warning + auto arity support #110
Conversation
@bartkrak could we also support passing a function without arity, that would match any arity? For example |
@mat-hek It's done. Now you can use macro in two ways: |
lib/unifex/specs.ex
Outdated
config | ||
|> Keyword.get_values(:dirty_functions) | ||
|> List.flatten() | ||
|> Enum.map(fn {dirty_func, type} -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of map and reject, use flat_map
lib/unifex/specs.ex
Outdated
{dirty_name, dirty_arity} = | ||
cond do | ||
is_tuple(dirty_func) -> | ||
dirty_func | ||
|
||
is_atom(dirty_func) -> | ||
{dirty_func, nil} | ||
end | ||
|
||
{matched_name, matched_arity} = List.keyfind(functions_arity, dirty_name, 0, {nil, nil}) | ||
|
||
if matched_name == dirty_name and (matched_arity == dirty_arity or dirty_arity == nil) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{dirty_name, dirty_arity} = | |
cond do | |
is_tuple(dirty_func) -> | |
dirty_func | |
is_atom(dirty_func) -> | |
{dirty_func, nil} | |
end | |
{matched_name, matched_arity} = List.keyfind(functions_arity, dirty_name, 0, {nil, nil}) | |
if matched_name == dirty_name and (matched_arity == dirty_arity or dirty_arity == nil) do | |
dirty_name = | |
case dirty_func do | |
{name, _arity} -> name | |
name -> name | |
end | |
{matched_name, matched_args} = List.keyfind(functions_args, dirty_name, 0, {nil, nil}) | |
if dirty_func in [matched_name, {matched_name, length(matched_args)}] do |
slightly more idiomatic, and you don't need functions_arity
lib/unifex/specs.ex
Outdated
|> Enum.unzip() | ||
|
||
{functions_args, functions_arity} = Enum.unzip(functions_args_arity) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{functions_args, functions_arity} = Enum.unzip(functions_args_arity) | |
functions_arity = Enum.map(functions_args, fn {name, args} -> {name, Enum.count(args)} end) |
You don't have to create functions_args_arity
variable, you can just create function_args
the way it has been done before your changes and create functions_arity
with a simple Enum.map
, without second call of Enum.unzip
, IMO it will simplify your code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after changes suggested by @mat-hek this is no longer necessary
lib/unifex/specs.ex
Outdated
{matched_name, matched_args} = List.keyfind(functions_args, dirty_name, 0, {nil, nil}) | ||
|
||
if matched_name != nil and matched_args != nil and | ||
dirty_func in [matched_name, {matched_name, length(matched_args)}] do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mat-hek I needed to add these nil checks to your suggestion, otherwise if List.keyfind()
doesn't match length(matched_args)
results in a crash
Co-authored-by: Mateusz Front <[email protected]>
solves: membraneframework/membrane_core#757