Defstruct ~w[..]a

Where is the syntax

defstruct ~w[..]a

defined ?

1 Like

Here ~w[foo bar]a compiles to [:foo, :bar]

iex> ~w[foo bar]a
[:foo, :bar]

See docs for sigil_w/2

So the example from docs:

defmodule Post do
  defstruct [:title, :content, :author]
end

could be written as

defmodule Post do
  defstruct ~w[title content author]a
end
6 Likes

Thanks; just to double check, this actually has nothing to do with defstruct right? These ‘sigils’, they’re almost like reader macros, they transform the list/object that comes after it into another Elixir object ?

2 Likes

this actually has nothing to do with defstruct right?

right.

These ‘sigils’, they’re almost like reader macros

In fact they are macros for our convenience! =)

5 Likes

To be more correct: They transform the ast or string given into some elixir term, depending on if they’re implemented by a macro or by a function.