Sebb

Sebb

Struct without fields

I just tried to create a struct without fields (because: why not?).
It’s not possible with defstruct, which insists on an argument.

Best it can do is

defstruct _: nil # nice

btw, this is not OK:

defstruct [_]

isn’t that inconsistent?

So I tried to figure out what defstruct does. From the docs:

To define a struct, a developer must define both __struct__/0 and __struct__/1 functions. defstruct/1 is a convenience macro which defines such functions with some conveniences.

Where are __struct__/x documented?

Structs are usually defined with the Kernel.defstruct/1 macro

What if I want to be unusual? How can I define a struct by hand, maybe even without fields?

Marked As Solved

NobbZ

NobbZ

Using an empty list as argument to defstruct/1works for me:

iex(1)> defmodule S do
...(1)>   defstruct []
...(1)> end
iex(2)> %S{}
%S{}

Also Liked

aiwaiwa

aiwaiwa

One legitimate reason would be to implement a protocol without data.

Use case: postgrex/lib/postgrex/parameters.ex at v0.16.5 · elixir-ecto/postgrex · GitHub

dimitarvp

dimitarvp

…but what’s your goal?

sheharyarn

sheharyarn

Like @Nobbz said, you can just do this:

defmodule X, do: defstruct []

The reason this doesn’t work is that you cannot assign _. The first example works because :_ is not _, so you can do defstruct [:_] which is equivalent to your first example.


This is the macro that’s called underneath when defining a new struct. Going over the definition should help you better understand manually defining a struct in a module.

Last Post!

aiwaiwa

aiwaiwa

One legitimate reason would be to implement a protocol without data.

Use case: postgrex/lib/postgrex/parameters.ex at v0.16.5 · elixir-ecto/postgrex · GitHub

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement