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

Where Next?

Popular in Questions Top

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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement