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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement