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

New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement