Fl4m3Ph03n1x

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?

  1. Do you simply define a map inside the order_info typedstruct and ignore the mandatory parameters for the user map?
  2. Do you create another typedstruct (called user) that goes inside the typedsctruct called order_info?
  3. What is the standard option in Elixir ?

Showing Posts 1 to 6

Eiji

Eiji

Looks like TypedStruct supports all typespec in Elixir. If that’s true then depending on your needs you can use a map notation or reference to other type - just like you do with String.t(). I do not see a problem if referenced module would also use TypedStruct.

xpg

xpg

I’m also a great fan of TypedStruct (and its cousin TypedEctoSchema).
I would definitely go for defining a User struct.
The reasoning for doing that would be the same as for creating the OrderInfo struct in the first place: Ensuring that mandatory fields are present, and make it clearer what is passed around.
If User is only used in the context of OrderInfo, 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

Fl4m3Ph03n1x OP

Quite interesting !
Do you also add a new function 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

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:

lib/
  order_info/
    user.ex
  order_info.ex

order_info.ex would then contain something like this:

defmodule OrderInfo do
  use TypedStruct

  typedstruct do
  ..
  end

and, order_info/user.ex:

defmodule OrderInfo.User do
  use TypedStruct

  typedstruct do
  ..
  end
dimitarvp

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 NimbleOptions in 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 NimbleOptions and 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

xpg

NimbleOptions looks interesting. I will check that out. Thank you for the pointer.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kpanic
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
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 Top

JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews