zachdaniel
Creator of Ash
I was hoping I could get something like this working:
defmodule FooRegistry do
@foos [
FirstFoo,
SecondFoo,
ThirdFoo
]
@foos_by_another_name Enum.into(foos, %{}, fn foo -> {foo.another_name(), foo} end)
@type foo [Enum.map(@foos, fn foo -> foo.t end)]
@spec list_foos() :: [foo]
def list_foos() do
@foos
end
@spec foo_from_another_name(atom) :: foo | nil
def foo_from_another_name(name) do
Map.get(@foos_by_another_name, name)
end
end
I’m not really worried about the exact syntax, but how might I go about dynamically generating a type like this?
Thanks!
Trending in Questions
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
I guess the type declaration would somehow need to actually be separated by
|so thatEnum.mapwould naturally not work.Eiji
@zachdaniel: You need to write helper module with some macros and then call them to generate ast for special module attributes like
@type.For
@typeyou need to return ast in format:Where for
childrenyou have same rules as for root element, so for 3 foos:and this ast for you will look like:
FirstFoo.t | SecondFoo.t | ThirdFoo.tWhat you need is to write recursive function that retruns Elixir AST in Tuple notation.
zachdaniel
Woah. Thanks for the tip! I wonder if people would wnat to do this more often if it was provided by dialyzer.
for shorthand or something. I’ll look into implementing something like this. Thanks!
evadne
For posterity:
kenny-evitt
Is there a way to wrap that in a macro, ex.
union_type/1, so that one could write:I haven’t been able to figure out how to write such a macro. This is the best I’ve been able to do so far:
(I added the
Enum.reverse/1call so that the order of the literal values, e.g. in@keys, matches the typespec for the generated typekey.)Eiji
First of all instead of
reverse+reduce(2 iterations) you should use[head | tail](1 iteration) like:Secondly you can use
@typedirectly in macro, for example:with this you have:
kenny-evitt
Thanks @Eiji!
I like your recursive definition but I’m not sure it’s much clearer – it doesn’t seem particularly sensible to support a single type for a ‘union type’ macro. Avoiding two iterations tho is pretty nice! And the function could always be made private anyways.
I suspected that it would be much easier to write a macro for the entire type definition – I couldn’t get one for just the ‘spec’ portion to work and suspect, if it is possible, that it would involve much more convoluted code.
Is it necessary to do the above in a
__using__macro specifically? (I’ll test that myself when I get a chance.)Eiji
In both
@specand@typeyou need to wrap everything inunquotecall. Without that instead of calling function/macro theASTof such call would be stored:__using__/1macro is as same as any other macro.use MyModuleworks likerequire MyModule+MyModule.__using__([]).You can also write a macro like this one:
However for this you need to add this option:
to
.formatter.exs, because otherwise it would format your code to:kenny-evitt
That’s the conclusion I tentatively had reached myself but do you know why exactly this is? I’d guess it has to do with the details of how, and when, the compiler parses the
@specand@typedeclarations, but I haven’t found any info about what those details might be. I’m just curious!Thank you for all of your help with this! I really appreciate it
What’s your personal preference for doing this kind of thing? Do you not do anything like this in your own code?
I kind of think that the ‘original’ is clearest (even if a bit more verbose):
But your
union_type/1macro is really nice too! I’d be a little worried that it’d be harder to read, e.g. easier to skip when scanning code that uses it and not notice that it’s declaring a type.The pattern matching trick that effectively allows you to use the
x :: ysyntax is brilliant! That seems like a really useful trick generally for writing nice macros.Eiji
Depends on situation … in private API I would use just
unquotecall in@specfor simplicity. However in order to create a nice public API I would write a simple macro and document it as this way is easier to read and understand for other developers.That’s why I mean …
private==verboseandpublic==nice APIGood documentation is solution for most problems.
If you or somebody else from your team would not like to remember such details then simply list all things like that in special markdown file. With that if you fail to find something take a look at such file and remind what you forgot. For example nobody needs to understand what
~>operator is doing, but documenting that your project usesokhex library forces current developers to remind about it and every new developer to read concepts of such library.If you want to generate something firstly check how to do it using
quotecall: