beno

beno

Is this feasible somehow?

Foo.bar()  #=> "bar"
Bar.foo() #=> "foo"

Context.perform do
  Foo.bar()  #=> "context_bar"
  Bar.foo() #=> "context_foo"
end

Showing Posts 1 to 8

OvermindDL1

OvermindDL1

I’d never recommend doing different things in different contexts without explicitly passing in the context, but there are a number of ways, passing in hidden data in the process dictionary, making perform be a macro that does different things to calls, etc… There are lots and lots of different way, almost all of them are not suggested. :slight_smile:

If you have a more specific complete example that we could fill in a few bits for you, then we can show you, but again, not recommended. ^.^

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This looks like a classic X Y problem (http://xyproblem.info/). What are you trying to accomplish in general?

There are a few options that look like that, each is a bit different in what it entails.

Pool.checkout(fn worker ->
  # now I can do stuff with the worker
end)
Repo.transaction(fn ->
  # stuff here executes in a transaction
end)
object :user do
  field :name, :string
  # this is a macro based DSL
end

Each of these works differently, and is used for different purposes. We need to know about what you’re trying to accomplish broadly speaking so that we can direct you in the right way.

beno

beno OP

It is the second case, I am working on a database driver named xarango. I am looking to see if I can use the same API for direct calls (producing HTTP request/responses) and calls inside a transaction (producing javascript code to be run on the server). But based on my insights so far I think I need a dedicated API for transaction calls.

OvermindDL1

OvermindDL1

Actually that might be a good case for using the process dictionary. If the transaction call happens in another process (whether the repo process itself or a new one) the transaction call can decorate the process by setting a key in the process dictionary, then your calls can just test for that.

I’m still not a fan of differences in operation based on state like that, but as long as it acts the same inside and outside the transaction, it makes sense.

beno

beno OP

Thank you for your suggestion. Process.put/2 and Process.get/1 indeed look like they provide the ‘global state’ I could use.

I would have to store a fairly complex and frequently accessed %Transaction{} struct in there though, since I need to concatenate subsequent transaction results and execute them as a whole. Besides all this becoming a bit un-functional, are there any (perhaps performance) objections to this?

OvermindDL1

OvermindDL1

Not at all functional (maybe could be considered some implicit ‘this’ maybe?), but it should be fast enough as when structs are changed then anything unchanged is handed over straight, only changed parts and the parts that contain them are recreated.

Do note, the process dictionary is very fast, generally only used as an optimization thing, or for doing non-functional stuff like this. ^.^

beno

beno OP

I think I will just make a dedicated Transaction API after all. This keeps me on the functional straight and narrow and gives me optimal flexibility for when the nasty details appear. :slight_smile:

— All posts loaded —

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