jc00ke
Expanding on this topic: Map typespec question
Let’s say I have a map with required and optional keys. I’d like to document both, but based on the docs it seems like you can’t denote a specific key is optional. Where else do people document these optional keys?
@type params :: %{foo: String.t, optional(:atom) => integer()}
@doc """
Does a thing
## Examples
iex> do_thing(params)
{:ok, "thing_done"}
## Parameters
%{
foo: required(string),
bar: optional(integer)
}
"""
@spec do_thing(params) :: {:ok, String.t}
def do_thing(...), do: {:ok, "thing_done"}
With that example in mind, how do I specify in the @type params declaration that :bar is optional? Just leave it out and document its optionality in the @doc?
Thanks for the help!
Trending in Questions
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
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
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
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
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
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
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
blatyo
Perhaps do it with the value.
Or maybe:
NobbZ
You already have the key
:atommarked as optional. So what do you want more?jc00ke
I actually want
optional(:bar) => integer()but AFAICT from the docs I have to specify thekey_typenot thekey.jc00ke
Is close, but that still looks like
:baris a required key even if the value of:barisnil. Those seem like different things.If I pattern matched the function args,
:barwould not be present:NobbZ
Literals are their own type. So
optional(:bar)is exactly that.jc00ke
Ah, yes! I see that now. Makes sense. Thanks!
jc00ke
Since I have your attention (
) is there a way to specify something is required if another key is not present? In this case, you can either have a
:counterparty_idor several:receiver_*pairs.NobbZ
No, neither the typespec syntax nor dialyzers checks can deal with mutual exclusion of keys.
OvermindDL1
Yep, use the union operator
|.I.E. you specify the whole structure twice with each variation, like:
It’s a bit of a pain and combinatorially explosive, but it works.
soup
A potential stumbling block: even though the keys look like atoms, you can’t mix the
x:and=>when usingoptional. I assume this is becauseoptional(:bar)resolves to a non-atom type.You might get the somewhat cryptic error:
You must convert to:
E: see below, or