fireproofsocks

fireproofsocks

Overriding non-defoverridable functions generated by use macro?

Is there any way to override functions generated by a use macro when those functions have NOT been tagged as defoverridable?

I’m tapped into a 3rd party package which does mostly what we want, but we need to do some tweaking of the return values to make things work with our particular stack.

The easiest solution is of course to define our own variant of the function under a different name, e.g.

defmodule OurModule do
  use Something

  def something_alternate(opts \\ []) do
        # custom stuff
        something(opts)
  end
end

But it’s a bummer to not be able to avail ourselves of the nice interface in the 3rd party package, so I was wondering if it would be possible to somehow override the functions that it generates.

For example, maybe we could roll our own defoverridable via something like this?

defmodule Wrapper do
  defmacro __using__(opts) do
    quote do
      use Something

      defoverridable Module.definitions_in(__MODULE__)
  end
end

Curious if anyone has tried this or can offer words of wisdom before I try this out in the laboratory…

Most Liked

LostKobrakai

LostKobrakai

You can, and you don’t even need to wrap it in your own use. You can just put the defoverridable inline in the module where you need that.

fireproofsocks

fireproofsocks

Not sure I follow that – where exactly? If I do this:

defmodule OurModule do
  use Something
  defoverrideable something: 1

  def something(opts \\ []) do
        # custom stuff here
  end
end

It gets a compilation warning:

warning: this clause for something/1 cannot match because a previous clause at line 5 always matches
  path/to/file

Last Post!

fireproofsocks

fireproofsocks

Hahaaa … thankyou. Another case where the problem was between the keyboard and the chair.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement