grych

grych

Creator of Drab

Help with a good API for a function

Hi all,
I am struggling with an API for very important function in my library, maybe someone will find a better idea than mine.

Background: there is a function poke, coming with arity of 2, 3 or 4, specs are:

@spec poke(Phoenix.Socket.t(), Keyword.t()) :: result
def poke(socket, assigns)
  
@spec poke(Phoenix.Socket.t(), String.t(), Keyword.t()) :: result
def poke(socket, partial, assigns)

@spec poke(Phoenix.Socket.t(), atom, String.t(), Keyword.t()) :: result
def poke(socket, view, partial, assigns)

Sample:

poke socket, assign1: "something", assign2: 42

I need to add an optional option, also the keyword list (although so far it will be only one option, :using_assigs). I could just use one key from the assigns list for this, so user would mix the assigns and options:

poke socket, assign1: "something", assign2: 42, using_assigns: [...]

Because assigns keyword list key are Phoenix template assigns, I could even prevent having using_assign by user (I compile the template and may raise when some uses this assign).

This would do a trick, but I am having the impression that it is not very idiomatic. Is there the cleaner way to solve this, without changing the existing API?

Most Liked

OvermindDL1

OvermindDL1

The 2 and 3 arity versions seem like excellent candidates to me to just become some of those ‘optional arguments’ though. :slight_smile:

poke(socket, assigns, [view: view, partial: partial | other_options]) :slight_smile:

OvermindDL1

OvermindDL1

I would honestly just put the assign/user arguments and the ‘optional’ argument into 2 different lists. I’m not a fan of the ‘hidden’ list that Elixir makes by default for trailing keyword arguments syntax and prefer it explicit, but personally using elixir’s syntax I’d prefer the user assigns to be in a list that is not at the end, and the optional arguments being at the end. User data that is in list format should always be represented as being a list, no surprises.

grych

grych

Creator of Drab

Yep, they are optional. And yes, I will probably use them. I am lucky enough I compile the template before, so if some uses my name of options as the assign name, I can just refuse to compile it.

Still, I am not very happy with this one :wink: but it is probably the cleanest way. Thanks a lot!

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement