apoorv-2204

apoorv-2204

I have a module utils,which is supposed to be used by MOdule A, B,C,D… and so on. The issue is utils modules have lot of constants, constants specific to module A,B,C,D … . When i call module utils functions from module A, I want those functions to use constants defined in module A. same for B,C,D,… .What to use Import, use,etc.

So I can call module utils functions, so those util module functions can load constants depending upon which module has made the call.Opposed to passing as variable to the utils module functions. (since there is lot of functions).

So how to determine which module has invoked a function in another module at runtime. So I can leverage that information.

Showing Posts 1 to 5

sodapopcan

sodapopcan

There is discussion about this here.

I can’t quite visualize what you’re trying to do without seeing code, but if you have code like this:

defmodule A do
  @module B
end

I would advice against that since it results in B being a compile time dependency of A. If you have a lot of stuff like this, compilation can start to get really slow if you aren’t careful about your dependency graph. Otherwise—carry on.

apoorv-2204

apoorv-2204 OP

defmodule Z do
@cons some_val 
#to be determine by caller module
    def func() do
       do_something_to(@cons)
     end
end
defmodule A do
@cons some_val
# or define it by some other means
    def do() do
      Z.func()
     end
end
defmodule B do
@cons some_val
    def do() do
      Z.func()
     end
end
defmodule C do
@cons some_val
    def do() do
      Z.func()
     end
end

It can be resolved by passing this extra variable from module, but issue is the function which use this constants are deeply nested, will require to add parameters to all already existing funcs

sodapopcan

sodapopcan

I can’t quite picture the nesting but it seems like you might be trying to re-create inheritance here?? There are likely better ways to do what you want to do though not sure how to advise. Hopefully you figured it out via that other topic!

benstepp

benstepp

It’s possible to do something similar with macros, but fair warning the general rule for macros is not to use them.

defmodule Util do
  defmacro do_something() do
    quote do
      Util.do_something_with(@constant)
    end
  end

  def do_something_with(constant) do
    IO.puts("The constant is #{constant}")
  end
end

defmodule A do
  require Util
  @constant 1

  def test() do
    Util.do_something()
  end
end

defmodule B do
  require Util
  @constant 2

  def test() do
    Util.do_something()
  end
end

defmodule C do
  require Util
  @constant 3

  def test() do
    Util.do_something()
  end
end

A.test()
# The constant is 1

B.test()
# The constant is 2

C.test()
# The constant is 3

al2o3cr

al2o3cr

I’m not going to even speculate along this line, because you shouldn’t do this.

Let’s talk about the underlying requirement instead.

Can you be more specific about what you mean by “having constants”? Modules don’t really “contain” anything - for instance, there’s no function in Module to get [Foo.Bar, Foo.Baz, Foo.Wat] given just Foo.

A common pattern for this is two parts:

  • functions that accept the module as an additional argument beyond the “public” API
  • a use macro that creates functions that include this extra argument in a user-controlled namespace

An example of this is Ecto.Repo.insert:

https://github.com/elixir-ecto/ecto/blob/f6ddc64fbb19588375137f70eea40bfaabf93804/lib/ecto/repo.ex#L304-L313

The underlying function is Ecto.Repo.Schema.insert, which accepts the repo as a first argument. In a default configuration with nobody calling put_dynamic_repo, that argument will be the module that said use Ecto.Repo.

In your case, you’d have a module in Utils that defines the __using__ macro and then use it in each of A, B, C, D like:

defmodule A.Utils do
  use Utils, some_config: "specific to A"
end

# in other A code:
A.Utils.do_stuff("x", "y")
# which then calls
Utils.do_stuff(A, "x", "y")
— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews