mudasobwa

mudasobwa

Creator of Cure

The post covering how to generate nifty types to use in @spec in compile time with macros.

Showing Posts 1 to 8

NobbZ

NobbZ

I’ve only skimmed it, but couldn’t find any parameterized type in there, all defined types are of arity 0, have I missed something?

mudasobwa

mudasobwa OP

Creator of Cure

It does not matter what arity are types, I meant one could parametrize call to use Scaffold with types, not parametrized types themselves.

Parameteized types would be handled for granted. I have removed “parametrized” from the tagline to avoid misunderstanding, thanks.

Qqwy

Qqwy

TypeCheck Core Team

I think the code you are presenting in the post can be simplified greatly.
I believe the following works:

defmacro __using__(opts) do
  fields = opts[:fields]
  keys = Keyword.keys(fields)
  fields_with_struct_name = [__struct__: __CALLER__.module] ++ fields

  quote location: :keep do
    @type t :: %{unquote_splicing(fields_with_struct)}
    defstruct unquote(keys)
  end
end
mudasobwa

mudasobwa OP

Creator of Cure

But this is almost exactly what I present in Working Solution. Yes, I agree, that maybe unquote_splicing/1 migth to some extent look more exquisite, but I explicitly wanted to show a low-level approach, dealing with real AST instead.

Also, this approach wouldn’t allow propagated types (:version in my example,) because version: atom() outside of the quote would raise.

Qqwy

Qqwy

TypeCheck Core Team

But what about adding them outside of the quote, rather than adding them to the quote you are writing anyway? Or maybe you might write them in their own dedicated quote before the final quoted AST that is returned?

The main reason to use the explicit AST-notation is to pattern-match on it (which you cannot do with quoted fragments because the meta fields of {name, meta, arguments} would often not match). In places where you are constructing AST it is virtually always more understandable to work with quote/unquote, in my humble opinion :slightly_smiling_face:.

mudasobwa

mudasobwa OP

Creator of Cure

Eh. {^name, _, ^arguments} = ast?

Anyway, this is the top of the iceberg only, because the approach shown would fail if the arguments are not hardcoded (compile-time literals,) but e. g. passed as a module argument (use Scaffold, @params.)

I have this problem overcome as well, and for it, I was not able to stay high-level not descending to AST, but mentioning this would have made the text too huge. I am planning Part II. Down the rabbit hole, so stay tuned :slight_smile:

Qqwy

Qqwy

TypeCheck Core Team

I meant more like for instance:

case expr of
   {:+, _, [lhs, rhs]} -> # ...
   {:my_fun, _, args} -> # ...
   # etc
end

Very true :blush:

I’m looking forward to it!

mudasobwa

mudasobwa OP

Creator of Cure

For the sake of consistency I posted it here: Elixir Blog Posts - #627 by benlime

— All posts loaded —

Where Next? Top

Trending in Guides/Tuts Top

c4lliope
# ~/src/livebook/.iex.exs System.cmd("xdg-open", [ LivebookWeb.Endpoint.access_url() ] ) Because Livebook requires a unique passcode on ...
New
krasenyp
You probably already know that <span>{nil}</span> in a HEEX template produces <span> </span> when rendered. I fin...
New

Other Trending Topics Top

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews