Search code examples
elixirrecordnewtype

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?

Solution

  • 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: https://elixirforum.com/t/how-to-define-macro-for-a-new-type/46852/10?u=fl4m3ph03n1x

    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