wwaldner
Defstruct inside __using__`
Is it possible to define a struct inside a using
defmodule Structor do
defmacro __using__(_) do
quote do
defmodule __MODULE__ do
defstruct [:field]
end
def create(field) do
%__MODULE__{field: field}
end
end
end
end
defmodule MyStruct do
use Structor
end
Marked As Solved
sodapopcan
You don’t need the defmodule __MODULE__ do part:
defmodule Structor do
defmacro __using__(_) do
quote do
defstruct [:field]
def create(field) do
%__MODULE__{field: field}
end
end
end
end
defmodule MyStruct do
use Structor
end
iex> MyStruct.create(:foo)
%MyStruct{field: :foo)
3
Also Liked
wwaldner
Yes, I had this originally but was was not able to get it to work because I had something else wrong. Thanks for pointing out that it should work.
1
Popular in Questions
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
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
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
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 record...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Other popular topics
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
Hi!
In PHP: $SERVER['SERVERADDR'] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New








