stevensonmt

stevensonmt

Defining types for struct fields

I would like to have a struct in a module without default values for the fields, but I would like each field to be limited to a specific type. Is there a way to define and enforce this in Elixir?

Most Liked

lud

lud

You can define a typespec for your struct but you cannot enforce it.

What you can do in your module is to use a constructor-like function:

def new(props) do
   ... validate the values
   struct(__MODULE__, props)
end

(new is not a special name)

hauleth

hauleth

I think that in this case struct!/2 (with bang) will make more sense.

lud

lud

I hesitated because if you validate the values beforehand you should not have to use it, but yes it may be safer.

@stevensonmt the difference is that struct!/2 will raise if the given props have extra properties or if keys declared with @enforce_keys are not defined.

Last Post!

RudManusachi

RudManusachi

It depends. While, IMHO, it’s easier to begin with case do, if the body of different branches of case is getting larger it’s nicer to break it into multiple functions. Plus multiple functions if fail - will give you better backtrace info on where to look at. Also, to me, it’s somewhat easier to read when the whole body of the function describes one path rather than keeping condition context in mind.
Or, just whatever is easier to read =)

Where Next?

Popular in Questions Top

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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement