How to combine @type?

Hello,

Is there a way to combine types? It would be awesome to be able to do |> for Map.merge

defmodule JEC.Types do
  @type network_identifier :: %{
          network_identifier: %{
            network: String.t()
          }
        }

  @type public_key :: %{
          public_key: %{
            hex: String.t()
          }
        }

  @type derive_account_identifier :: network_identifier() |> Map.merge(public_key())
end

When i try the above i get this error

(CompileError) type |>/2 undefined (no such type in Radixir.Types)
Stacktrace:
  │ (elixir 1.13.2) lib/kernel/typespec.ex:923: Kernel.Typespec.compile_error/2
  │ (elixir 1.13.2) lib/kernel/typespec.ex:307: Kernel.Typespec.translate_type/2
  │ (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
  │ (stdlib 3.17) lists.erl:1359: :lists.mapfoldl/3
  │ (elixir 1.13.2) lib/kernel/typespec.ex:235: Kernel.Typespec.translate_typespecs_for_module/2

But doing this work

defmodule JEC.Types do
  @type network_identifier :: %{
          network_identifier: %{
            network: String.t()
          }
        }

  @type public_key :: %{
          public_key: %{
            hex: String.t()
          }
        }

  @type derive_account_identifier :: Map.merge(network_identifier(), public_key())
end

Im going to need to merge a lot @type so doing the Map.merge would get pretty messy.

Any ideas / help would be much appreciated.

Not going to give you a copy-paste-able suggestion but probably macros can help.

Cool - ill dig into macros :call_me_hand:

Look into the Code.Typespec

https://github.com/elixir-lang/elixir/blob/d17d2f209113143c32c72b8815e7d9fb2dc93527/lib/elixir/lib/code/typespec.ex

Note it is not an API,
so copy and paste the code and base your code in it, as it may change over time.

1 Like

Note that you can also do a join,

@type derive_account_identifier :: network_identifier() | public_key()
1 Like

Always thought that’s an OR operator. But it actually merges? Nice.

I don’t believe it merges :confused:

I think it is intact an OR

1 Like

exactly.