ketupia

ketupia

I find myself writing this code repeatedly. Is there a better way

  if Status.exists?(entity) do
    Status.update(entity, new_value)
  else
    Status.add(entity, new_value)
  end

Showing Posts 1 to 9

tfwright

tfwright

Can you provide a bit more context about what could be “better” in your view? Obviously you could wrap that code in a function and call that function instead, but I assume you are looking for something a bit more sophisticated?

ketupia

ketupia OP

Something out of the box provided by the ECSx.Component.

  • maybe add the wrapper method you suggested to the ECSx.Component macro

  • maybe update would add it if it didn’t exist. If there’s a need to raise errors from update if doesn’t exist, include an update! function

  • maybe add would update it if it did exist. If there’s a need to raise errors from add if it existed, include an add! function

smathy

smathy

Ironically, :ets has only a single insert that either adds or updates (overwrites). As you can see in the sauce the main difference between add and update is essentially just a guard to prevent existing elements from being overwritten, or non-existing elements from being auto-created.

There’s no upsert operation, might be worth submitting a PR.

APB9785

APB9785

Creator of ECSx

As @smathy pointed out, :ets inserts generally allow overwrites, and this was originally how ECSx worked as well, with just a single add operation that covered both cases. The reason we separated into two functions, is because of component persistence. Take the following case:

HitPoints.add(player, 100, persist: true)
HitPoints.add(player, 95)

The first operation inserts a persistent HitPoints component with value 100
The second operation overwrites this with a non-persistent (default is false) HitPoints component with a value of 95.

What was the problem? If a user provides options to the component creation, they would have to remember those options and pass them to every update, forever. Forgetting to include the original options would overwrite with the defaults. We considered setting the options globally, but quickly found use-cases where a single component type (such as HitPoints) would be persisted for some entities, but non-persisted for others. And we want to keep the door open for future options to be added.

So the end result is that we need one function for the original creation of a component, where the options are set, and then another function for updating the component, where no options = keep the existing options.

smathy

smathy

Makes sense. Did you consider storing the opts in ETS along with the value?

APB9785

APB9785

Creator of ECSx

The opts are stored in ETS. It’s not an implementation issue, it’s an API issue - with only one function for both add and update, it becomes ambiguous whether a user is leaving off the options because they want the options to stay the same, or if the lack of options means that they want the defaults. The only way to ensure consistency would be to demand that users include the options every time.

smathy

smathy

Oh I see what you’re getting at. I’m not sure people would expect that though (ie. that leaving options off for an existing entity would change it to the default).

Also, right now the current API doesn’t let them change the options of an existing entity at all, so you wouldn’t lose any functionality with an add that just left the opts as-is for an existing entity when the caller left them off.

Then if they wanted the opts updated on an existing entity, they’d provide the opts, which would be additional functionality.

I think the problem you saw was maybe doing an :ets.insert without doing a :ets.lookup. Obviously with that implementation you’d be clobbering existing opts for an update if they hadn’t provided them.

Or am I just missing something dumb here? :slight_smile:

APB9785

APB9785

Creator of ECSx

Also, right now the current API doesn’t let them change the options of an existing entity at all

This isn’t guaranteed to be the case forever.

so you wouldn’t lose any functionality with an add that just left the opts as-is for an existing entity when the caller left them off… if they wanted the opts updated on an existing entity, they’d provide the opts, which would be additional functionality.

If the default functionality is “keep the existing value” then it doesn’t work for creating new components (because there is no existing value yet). And we cannot have two different default functionalities for the same function, based on a runtime conditional. That would be very bad.

smathy

smathy

Understood.

— 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
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
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
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews