ilya-kolpakov

ilya-kolpakov

Structs: enforce_keys is not a good name

By defining a struct one guarantees that the instances’ keys are exactly those provided in the definition. By including a key within the enforce_keys attribute one ensures that the corresponding values cannot be nil.

In my view, the attribute name enforce_keys suggests that the keys listed there will be always present in the struct (which is actually the case for all keys provided in the definition) rather than that the corresponding values cannot be nil.

Most Liked

chrismccord

chrismccord

Creator of Phoenix

Sorry, I was saying that your ideal view is the current behavior. For example:

iex(1)> defmodule A do
...(1)>   @enforce_keys [:name]
...(1)>   defstruct [:name, :age]
...(1)> end
{:module, A,
 <<70, 79, 82, 49, 0, 0, 8, 128, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 14,
   0, 0, 0, 27, 8, 69, 108, 105, 120, 105, 114, 46, 65, 8, 95, 95, 105, 110,
   102, 111, 95, 95, 9, 102, 117, 110, 99, ...>>, %A{age: nil, name: nil}}
iex> %A{age: 1}
** (ArgumentError) the following keys must also be given when building struct A: [:name]
    expanding struct: A.__struct__/1
    iex:3: (file)

iex> %A{name: nil}
%A{age: nil, name: nil}

Enforce keys doesn’t care about nil values, only keys. Does that clear things up?

ilya-kolpakov

ilya-kolpakov

Oh I see. So keys listed under enforce_keys must be present in the constructor and that’s all. They are, in a sense, like positional args to functions as they need to be always present.

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement