stevensonmt
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?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities.
Sup...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #elixirconf-us
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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:
(
newis not a special name)hauleth
I think that in this case
struct!/2(with bang) will make more sense.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!/2will raise if the given props have extra properties or if keys declared with@enforce_keysare not defined.stevensonmt
Thank you! If using
struct!/2it may raise an exception at runtime and crash, but if using the suggestednew/1constructor you can handle the exceptions gracefully, is that right?lud
No because as I’ve said,
newis not special, it is just a function that you have to define (you could call itcreateor anything else), and in that function (see my snippet) you call eitherstruct/2orstruct!/2. So it is up to you if you allow to raise.stevensonmt
Oh, I see,
structvsstruct!in the constructor function. Thanks!al2o3cr
AFAIK using either
structorstruct!doesn’t let Dialyzer “see” what’s happening, and that’s what’s doing the type-enforcement when you use something liketyped_struct. The only difference between the two is thatstructsilently ignores keys it doesn’t understand, whilestruct!raises.Writing a literal will complain, though:
%SomeStruct{key: "value_of_wrong_type"}.You could accomplish a similar result by carefully type-specing your
newfunction as well.brettbeatty
You can always reduce the opts into your struct however you want. You can validate, rename, transform, do whatever.
A simple example where opts of the wrong type are simply rejected
stevensonmt
That’s a great tip. Can I ask what the first
defp put_opt(opt, acc)without further definition is for?brettbeatty
Oh sorry it’s a personal preference when I create a function with multiple clauses to have that. In my opinion it makes it clearer what the args should be, often helps with docs (for public functions), and gives me somewhere to put
@doc,@spec, etc. It’s only really required when you have multiple clauses for a function that takes default arguments. Kernel — Elixir v1.20.2