homologist

homologist

Is there any way to, at compile time overrid ALL functions foo that somebody would wrote in a module A.

I think I can do it overriding def/2. But I feel like i would be using a hammer. Any ideas?

Showing Posts 16 to 7

dom

dom

If it’s for logging calls or computing statistics, you could use the standard tracing tools in Erlang’s dbg module.

NobbZ

NobbZ

Either you use the on definition callback to collect data which you apply in before compile, or you require your client to annotate each function explicitly either by listing them as argument to your useing or as in the decorator package.

There were a lot of discussions about decorators and only a handful of people actually wanted them, most said they are not necessary…

So the few that needed them created the package and still seem to maintain it.

homologist

homologist OP

Yes my client would have to call use.

Unlike other hooks, @on_definition will only invoke functions and never macros. This is to avoid @on_definition callbacks from redefining functions that have just been defined in favor of more explicit approaches.

I’m not sure if it apply to simple approach you guys suggested or just for dude who would like to insert code in a function. That’s why i didn’t dig deeper in this way

NobbZ

NobbZ

Your client has to be explicit somewhere about what you shall change.

Either he has to call explicitly your service per function, or he has to use moduleattributes to annotate the functions as in :decorator-package, or he has to tell when useing your module.

You should be able to use that information the client provided in a @on_defintion callback.

homologist

homologist OP

Thanks for the package, I did find some, but not this one.

On_definition, I understand prevent(or try to) redefining the actual function.

I try to have

defmodule A do
   use MyPlugin
   def foo, do: ...
 end

and under the hood it would compile like

 defmodule A do
   def foo, do:stuff1
   def foo, do:stuff2and stuff1
 end

The thing is, I’m not naming the function foo but the client of the module is. And me I’m just providing him a plugin.

NobbZ

NobbZ

A callback is implemented against a contract given by a behaviour, which dictates names and arities, so you now all you need…

Please check if the :decorator package suites your needs or if you can take some insights from it.

Maybe you can also pull some use from the @on_definition module attribute.

At the moment, I really can’t tell you anything more, since I simply do ot understand what you want to achieve… You say, you want to decorate all foo, but then you say you do not know what you want to decorate, and then again you say thats a callback, so you have a contract… I’m confused about your plans…

homologist

homologist OP

The log was just an example. Its just some callbacks

NobbZ

NobbZ

You neither know how the function will be named nor what arguments it might get? What do you actually want to log then?

NobbZ

NobbZ

Or better probably this

def foo(x, y, z) do
  log_stuff(...)
  do_foo(x, y, z)
end

defp do_foo(...)...

This way only the decorator is accessible and doesn’t have an obfuscated name.

homologist

homologist OP

I’m not supposed to write anything in the module A. So a client would go:

 defmodule A do
   def foo, do: ...
 end

and under the hood it would compile like

 defmodule A do
   def foo, do:stuff1
   def foo, do:stuff2and stuff1
 end

And my main problem is that i don’t know the name and arity of foo function. So it’s acutally hard to have any simple strategy like @kokolegorille suggested.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews