Any hackery for replacing references to a top-level module with references to a different one with the same API?

Let’s suppose that you’re me: a person who’s decided, for perhaps silly reasons, to write a package – let’s call it Lens2 – that is to be mostly backwards compatible with another one – call it Lens.

Existing Lens client code refers to the top-level package without aliases, as with Lens.key(:a). Clients of Lens2 can do the same, so long as use Lens2 is added to the module (because it contains alias Lens2.Lenses, as: Lens).

However, there’s a third-party package, TypedStructLens, that also makes direct references to Lens. It’s a super-simple package, so it’s trivial to copy and tweak it to work with Lens2, but it made me wonder:

Is there some magic where you can say, at “link time” or “symbol resolution time”: replace references to module Lens with Lens2.Lenses?

I would say the TypedStructLens should be used via, ahem, a use macro with options f.ex. version: :lens1 or version: :lens2 that only does the aliasing if :lens2 is chosen.

1 Like

There is not. Elixir is explicit and therefore require TypesStructLens to decide which module to alias or use. It cannot be affected from the outside (at least unless opted into such by TypesStructLens).

1 Like