Skip to content
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

Raise on keys for non existing variables in matches #4

Open
tomekowal opened this issue Aug 1, 2022 · 0 comments
Open

Raise on keys for non existing variables in matches #4

tomekowal opened this issue Aug 1, 2022 · 0 comments

Comments

@tomekowal
Copy link

tomekowal commented Aug 1, 2022

Hi! First of all: fantastic project!

Thanks you and sharing how the lib helps my usecase

I was writing a credo check. I needed to match on "pipe operator where the second param is function application". Those kinds of patterns are pretty big:

{:|>, a,
          [b, {{:., c, [{:__aliases__, meta, [module_name]}, function_name]}, d, params}]},

but using the lib makes them more manageable:

  defpat(pipe({:>, meta, [before_pipe, after_pipe]} = ast))

  defpat(
    function_call(
      {{:., dot_meta, [{:__aliases__, module_meta, [module_name]}, function_name]}, params_meta,
       params} = ast
    )
  )
  defp traverse(
         pipe(
           meta: a,
           before_pipe: b,
           after_pipe:
             function_call(
               dot_meta: c,
               module_meta: meta,
               module_name: module_name,
               function_name: function_name,
               params_meta: d,
               params: params
             )
         ),
         ...) do

Thank you very much for making composing pattern matches easier! I wanted to share with you how much easier it makes writing those checks :)

Actual issue

If I make a pattern like this:

  defpat ok({:ok, val})

but then misuse it like this:

  def my_fun(ok(value: value)) do
    value
  end

Notice that the binding is called val but used :value as the key in the keyword list. The error I get is:

warning: variable "value" does not exist and is being expanded to "value()", please use parentheses to remove the ambiguity or change the variable name

If I understand correctly, it means that the key value is ignored when there is no binding called this way. It might be a little bit confusing in case of typos to understand what is happening.

Would you consider it a good idea to raise in case usage of a given pattern tries to access non-existent binding?

If yes, I could try digging into it over the weekend and checking if I understand the lib enough to make that change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant