jc00ke

jc00ke

Typespec for map w/both required and optional keys

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!

Marked As Solved

NobbZ

NobbZ

Literals are their own type. So optional(:bar) is exactly that.

12
Post #5

Also Liked

OvermindDL1

OvermindDL1

Yep, use the union operator |. :slight_smile:

I.E. you specify the whole structure twice with each variation, like:

%{
  account_number: String.t(),
  :counterparty_id => integer(),
} | %{
  account_number: String.t(),
  receiver_account_number: String.t()
}

It’s a bit of a pain and combinatorially explosive, but it works. :slight_smile:

soup

soup

A potential stumbling block: even though the keys look like atoms, you can’t mix the x: and => when using optional. I assume this is because optional(:bar) resolves to a non-atom type.

You might get the somewhat cryptic error:

@type params :: %{
  foo: String.t(),
  optional(:bar) => integer() # "syntax error before: optional"
}

You must convert to:

@type params :: %{
  foo => String.t(),
  optional(:bar) => integer()
}

E: see below, or

@type params :: %{
  optional(:bar) => integer(),
  foo: String.t()
}
LostKobrakai

LostKobrakai

The syntax sugar for keyword lists as well as for atom keys in maps is only allowed trailing to elements not using the syntax sugar within the same map/list. Though I‘m not sure if the same does work in typespecs.

Last Post!

nathanl

nathanl

Thank you for posting this - I was confused about exactly this error message and found your post.

I’ve opened an issue: Unhelpful error for typespecs with inconsistent key syntax · Issue #10973 · elixir-lang/elixir · GitHub

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement