Fl4m3Ph03n1x
Background
I am moving towards defined data structures in my application, and I find that TypedStruct is quite useful.
Questions
However, I have recently found a little hickup. I don’t know how to convert this definition into a typedstruct:
@type order_info :: %{
(visible :: String.t()) => boolean,
(order_type :: String.t()) => String.t(),
(platform :: String.t()) => String.t(),
(platinum :: String.t()) => non_neg_integer,
(user :: String.t()) => %{
(ingame_name :: String.t()) => String.t(),
(status :: String.t()) => String.t()
}
}
My main issue here is the user map. Aside from the fact I don’t know how to define a typedstruct inside a trypedstruct, how do you guys do this?
- Do you simply define a map inside the
order_infotypedstruct and ignore the mandatory parameters for theusermap? - Do you create another typedstruct (called
user) that goes inside the typedsctruct calledorder_info? - What is the standard option in Elixir ?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
Looks like
TypedStructsupports all typespec inElixir. If that’s true then depending on your needs you can use amapnotation or reference to other type - just like you do withString.t(). I do not see a problem if referenced module would also useTypedStruct.xpg
I’m also a great fan of TypedStruct (and its cousin TypedEctoSchema).
I would definitely go for defining a
Userstruct.The reasoning for doing that would be the same as for creating the
OrderInfostruct in the first place: Ensuring that mandatory fields are present, and make it clearer what is passed around.If
Useris only used in the context ofOrderInfo, I would name it accordingly, i.e.OrderInfo.User.I have to admit that I try to use (typed)structs as often as possible – maybe even too often. But I like the fact that I can be explicit about expectations. I also try to ensure that I pattern match against the expected struct in the function heads that expect structs as input.
Fl4m3Ph03n1x
Quite interesting !
Do you also add a
newfunction to build the structs? Or do you simply build them manually?I understand you are advocating for nested module in this use case. Could you please be so kind as to share how your final solution for this would look like?
xpg
I’m actually quite undecided on that part. The OOP-developer in me really wants a
new-function, as it allows me to have the required fields as required parameters, and then an optional keyword-list or map as the last parameter to fill in optional fields. (It also allows fields to be calculated).However, lately I am questioning that approach for several reasons: First, the Elixir compiler is quite good at complaining about missing required fields. Second, that last optional parameter taking a map or keyword-list is really not nice to deal. Third, when you have more than three required fields, the approach results in a large number of parameters to the
new-function.Now I am trying to embrace structs as open (non-hiding) data structures. No constructors, and no calculated fields.
“Nested” in the sense of namespace, not in code, though. I really try to keep strictly to one-module per file. I would have the following directory structure:
order_info.exwould then contain something like this:and,
order_info/user.ex:dimitarvp
Agreed, just having a few layers of keyword options passed around, with defaults and some having to override others etc., it becomes a huge rabbit hole that’s an extremely easy vector to introduce bugs.
That being said, I’ve been trying to use
NimbleOptionsin my own hobby work lately and so far it’s serving me pretty well. Bonus points for also serving as kind of a typed schema as well, that was a nice and very welcome surprise.Yeah, that’s the biggest problem here, and that’s why I started using
NimbleOptionsand having only a single parameter – the keyword list – which in effect imitates the named function arguments that other languages enjoy (like OCaml). So far I like the result but it’s too early to say because my own hobby OSS work is going at a snail’s pace. Time will tell but tentatively, that library makes things possible and much less annoying.xpg
NimbleOptions looks interesting. I will check that out. Thank you for the pointer.