DaAnalyst
I may be wrong here, but I really don’t see a proper way to override an operator in one’s own library while allowing for a fallback to whatever library module the user imported before it that also overrides the same operator (or any other function or a macro), e.g.:
use OtherLibrary # imports its `*` override for maps
use MyLibrary # imports its `*`override for lists
2 * %{ a: 1}
# => MyLibrary passes it to the whatever prior library overriding `*` i.e. OtherLibrary and OtherLibrary does its job
2 * [ a: 1]
# => MyLibrary does its job
2 * 2
# => 4 (MyLibrary passes it to OtherLibrary which passes it to Kernel)
The idea is to fetch the last prior import by relying on Macro.Env.lookup_import/2, and it works, but I have another problem. All I can do with the import prior to mine in MyLibrary.__using__/1 is import unquote( prior_import), except: [ *: 2] and that’s not exactly what I need. The reason is I cannot afford to assume that the caller is ok with my library importing all other macros and functions from the OtherLibrary module (otherwise unknown to me), and I don’t know of any other way to un-import just this particular function or macro.
Any ideas?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
How about Kernel.defoverridable/1?
DaAnalyst
Never applied it in a case like this. This is not an “inheritance” case. You think it will work?
DaAnalyst
Nope, I can’t get it to work. Besides, even if it did work, it would require other libraries to define it too.
Eiji
It would be easier if you would give something from you like an example
Elixirscript or at least tell us what error you have.does not tell us anything.
DaAnalyst
Ok. For starts, here’s the sketch of the original that I explained above that works, but imports all functions and macros from a prior library (the operator is
-but it’s still the same point).DaAnalyst
Regardless of the
defoverridable/1, if I remove the lines withimport unquote( prev_import) ..it will complain about ambiguous call of the-operator. If I don’t remove the lines, I am importing all the functions and macros.benwilson512
I mean as a general rule, I would simply not try to do this at the module level. If a library wants to override an operator that should be lexically scoped inside one function at a time.
Eiji
There is no good solution for this. Look that if you do
import … except …then what happens with all previous imports?For example:
Firstly I would go for: Defining custom operators. and then I would register an (accumulate?) attribute
:prev_importand withinuse HelperI would get said attribute and define a custom function/macro depending on value of such attribute.Alternative both libraries could put an attribute and add
@before_compilewhich would work as same asuse Helper.DaAnalyst
Yes, and there’s also another solution and that is not to stop at just taking the prev_import but then also (while still in compile time) take all of the module’s macros and functions and exclude them all unless already imported, but it’s already getting cumbersome.
My point being:
importhad an additional option (say:not) to un-import some but not import all other functions/macros.Just sayin’
DaAnalyst
Your observation is valid if the library is a very specific one. But if you’re developing something of a very generic purpose that can be applied all over your code base, then evading this problem will hardly do it. Still, even if done on a function by function basis, I am simply reluctant of importing all of third party module’s functions/macros just because I need to exclude one.