dogweather

dogweather

Here’s a simple type for a piece of historical stock info:

defmodule DayRecord do
  @enforce_keys [:date, :close, :high, :low, :open]
  defstruct date: ~D[1900-01-01], close: 0, high: 0, low: 0, open: 0
  @type t :: %__MODULE__{date: Date.t, close: Float, high: Float, low: Float, open: Float}
end

I’m sure there’s more I can add, but I’m already a little unhappy with the redundancy and mix of different DSLs and implementation details.

Am I going down the right path?

I’ve seen a Hex package that wraps some of this up. Is there any traction around a certain way of removing the boilerplate? Maybe continuing defstruct’s example, and making a higher level macro?

Showing Posts 1 to 3

al2o3cr

al2o3cr

First thought: enforce_keys and default values are at cross-purposes; the defaults supply a value for when the key isn’t passed to %{}, but enforce_keys requires that the key be passed. Note that enforce_keys is NOT a “not null” constraint - it’s completely valid to pass nil for an enforced key.

If you have some fields that should always be passed but others that should be defaulted, you could extract some of the duplication:

defmodule StructDemo do
  @enforce_keys [:first_name, :last_name]
  @optional_keys [age: 0, heads: 1]
  defstruct @enforce_keys ++ @optional_keys
end

Usage:

iex(2)> %StructDemo{first_name: "Bob", last_name: "Dobbs"}
%StructDemo{age: 0, first_name: "Bob", heads: 1, last_name: "Dobbs"}

iex(3)> %StructDemo{first_name: "Prince"}
** (ArgumentError) the following keys must also be given when building struct StructDemo: [:last_name]
    expanding struct: StructDemo.__struct__/1
    iex:3: (file)

iex(3)> %StructDemo{first_name: "Nobody", last_name: nil}
%StructDemo{age: 0, first_name: "Nobody", heads: 1, last_name: nil}
ZsoltMaslanyi

ZsoltMaslanyi

Not OP, but that’s straight up beautiful. Thank you!

Adzz

Adzz

An alternative you can consider also is using Ecto (note you don’t need ecto_sql here either). In that case you can create an embedded schema like so:

defmodule DayRecord do
  use Ecto.Schema

  embedded_schema do
    field(:date, :date)
    field(:close, :decimal)
    field(:high, :decimal)
    field(:low, :decimal)
    field(:open, :decimal)
  end
end

As already mention enforcing the keys then adding defaults is a bit redundant, but if you want to add defaults you can in Ecto too:

defmodule DayRecord do
  use Ecto.Schema

  embedded_schema do
    field(:date, :date)
    field(:close, :decimal)
    field(:high, :decimal)
    field(:low, :decimal)
    field(:open, :decimal, default: Decimal.new(0))
  end
end

To me this makes it perfectly clear the types of each field. Then benefit then is you can use changesets and validations on creation to enforce things like non_null fields etc.

I’ve also been working on a library to give some sugar to this kind of use case called ecto_morph

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New
nseaSeb
I’ve just put together a small POC exploring PDF inspection from Elixir/Phoenix: The idea is pretty simple: drag & drop a PDF in a...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews