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

Override init/1 to pass options into the rule macro #21

Open
ribanez7 opened this issue Oct 14, 2021 · 1 comment
Open

Override init/1 to pass options into the rule macro #21

ribanez7 opened this issue Oct 14, 2021 · 1 comment

Comments

@ribanez7
Copy link

ribanez7 commented Oct 14, 2021

Is it possible to override the init function and make available those options into the rule macro?

Use case:

init/1 runs at compile time, and allows to set calculations into the options that cannot be computed in a module attribute definition. Lets say I want to compile some patterns in compile time using the :binary.compile_pattern(["some", "nice", "patterns"]) to later use them into some function. That cannot be done in a module attribute since it only support specific values easy to escape. Init would be the way to go then, but the options won't be available into the rule macro, and I don't won't to override also the call/2 function to assign those values into the conn, since that computation doesn't relate to the conn at all.

Thanks!

@ribanez7 ribanez7 changed the title Override init/1 to pass options into the rule Override init/1 to pass options into the rule macro Oct 14, 2021
@michalmuskala
Copy link
Owner

The values that are allowed in module attributes are also the same values that are allowed in compile-time init return values (since it ends up in a module attribute eventually). So i"m not sure this would actually help for the use case you talk about.

One way to solve it for patterns specifically could be to leverage the persistent_term module which seems ideal for things like that that never change. Something like:

pattern =
  case :persistent_term.get(__MODULE__, nil) do
    nil ->  
      pattern = :binary.compile_pattern(["some", "nice", "patterns"])
      :persistent_term.put(__MODULE__, pattern)
      pattern
    pattern ->
      pattern
  end

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

2 participants