Fl4m3Ph03n1x

Fl4m3Ph03n1x

Is there a way to have Elixir Records without default values?

Background

I am trying to find a cheap and easy way to create New Types in Elixir, and Records seem to be just what I would need.

Problem

However, Elixir records require one to define default values. Not only that, it also allows one to create empty records (which would then be populated with said default values).

For my specific use case, this is a problem. Not only don’t I have anything that can be used as a default value, I also don’t want to allow the users of my code to create empty records.

Now, I understand this is likely a well intended choice, most likely so it can interface nicely with Erlang records, but it causes an usability issue on my end: it allows the creation of non valid data.

Questions

I understand there is probably no solution for this conundrum using Records only, so I was wondering if there are alternatives in the wild of libraries or even hacks to accomplish this.

I personally have found nothing, right now I have the feeling my only solution is to write my own macro.

  • Is there a way to have Records not accept default values?
  • If not, what community libraries are out there that could help fulfill the role of creating a New Type?

Marked As Solved

Fl4m3Ph03n1x

Fl4m3Ph03n1x

Answer

  1. Is there a way to have Records not accept default values?

No. This is not possible with Records. Records were never intended for this use case and forcing this abstraction into them would only complicate things. While one could use a wrapper new method, it would still be a lot of boilerplate and all the validation for type would be on the user.

  1. At the time of this writing, there are none. However, in another post I created a macro that achieves this purpose: How to define Macro for a new Type? - #10 by Fl4m3Ph03n1x - Questions / Help - Elixir Programming Language Forum

In that post I propose an API and then I refine it with the community’s help. For those of you who are curious, it can be used like this:

type.ex

defmodule Type do
  import NewType

  deftype(Name, String.t())
end

test.ex

defmodule Test do
  alias Type.Name

  @spec print(Name.t()) :: binary
  def print(name), do: Name.extract(name)

  def run_1 do
    # dialyzer complains !
    Name.new(1)
  end

  def run_2 do
    # dialyzer complains !
    print("john")
  end

  @spec run_3 :: binary
  def run_3 do
    print(Name.new("dow"))
  end
end

Also Liked

OvermindDL1

OvermindDL1

If you want to validate data then make a constructor function for it, I generally call it new.

Where Next?

Popular Backend topics Top

Kurisu
Hello, Please, let’s say I have a website with user authentication made with Elixir/Phoenix, and now want to add a forum to it (using a ...
New
chasekaylee
Hi there everyone! Recently, I have fallen in love with programming with Elixir and have been having so much fun with it. I have been do...
New
dimitarvp
As a part of my new job I’ll have to learn to manage a local k8s cluster. The tools used are microk8s, tilt and helm. I’ll appreciate an...
New
New
sampu
I have a use case where a client is invoking a Rest endpoint via a load balancer, which in turn invokes a third party endpoint which is r...
New
conradwt
Hi, I’m building an application that will have support for both the web and mobile. At this time, I’m using PhxGenAuth for authenticatio...
New
Fl4m3Ph03n1x
Background I have a personal project that is an elixir desktop application for PC Windows. It works pretty well, but now I want to give i...
New
New
Fl4m3Ph03n1x
Background I have a release file inside a tarball. However I want the final release to have some additional files and to move things aro...
New
apoorv-2204
Anyone know how to get in golang? I am from elixir background?.
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1139 25478 754
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New