shahryarjb
Hi, I have Elixir macro that I want to use it as @behaviour in my project. but there is a problem I can not be able to use a @type as a parameters of a macro.
My macro
defmodule MishkaPub.ActivityStream.Validator do
defmacro __using__(opts) do
quote(bind_quoted: [opts: opts]) do
type = Keyword.get(opts, :type)
module = Keyword.get(opts, :module)
@type t :: unquote(type)
@type action() :: :build | :validate
@callback build(t()) :: {:ok, action(), t()} | {:error, action(), any()}
@callback build(t(), list(String.t())) ::
{:ok, action(), t()} | {:error, action(), any()}
@callback validate(t()) :: {:ok, action(), t()} | {:error, action(), any()}
@callback validate(t(), list(String.t())) ::
{:ok, action(), t()} | {:error, action(), any()}
@behaviour unquote(module)
end
end
end
And the Elixir file I want to use it
defmodule MishkaPub.ActivityStream.Type.Object do
alias MishkaPub.ActivityStream.Validator
@type tt :: %__MODULE__{
id: String.t(),
type: String.t(),
name: String.t(),
replies: list(String.t())
}
defstruct [
:id,
:type,
..
]
use Validator, module: __MODULE__, type: tt()
def build(%__MODULE__{} = params) do
{:ok, :build, Map.merge(%__MODULE__{}, params)}
rescue
_e ->
{:ok, :build, :unexpected}
end
...
end
but I have this error
** (CompileError) lib/activity_stream/validator.ex:3: undefined function type/0 (there is no such import)
How can fix this error?
Another problem if this is fixed! I have 2 duplicated types. (tt(), t()) but I just want to have one type which should be t()
Thank you in advance
![]()
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hst337
Please note that
@callbackattribute is set in the module which defines the interface. While@behaviouris set in the module which implements the interface.So you’ll actually need something like this
al2o3cr
+1 to what @hst337 said, the behaviour and the implementation need to be separate modules.
Some other thoughts:
passing
__MODULE__isn’t required, the module that saiduseis available as__MODULE__inside thequoteblock.apart from failing to compile, the only thing that
typeinValidatordoes is definet. What about just expecting the user to write@type t :: etc etc etcoutside of theuseand then using it in the@callbacks?shahryarjb
I just wanted to create a structure to force programer to do something I want and prevent duplicating code!! so I figured out I have some duplicated
@callbackin many modules like:The only thing is different in each modules of my project the first entry of my type I mean
t(), first of all I could usestruct()for all of them but it is kind of of usinganylike and I want to have a specific one.christhekeele
The folk in this thread are providing solid advice to improve your approach, which I would recommend following and would resolve your issues.
However, to address the specific cause of this error the way you are doing things today, for learning about Elixir metaprogramming:
You have an unquoting issue in your macro. Take just the lines here:
You have a variable,
opts, inside of your macro’s context, containing a keyword list. To make that available to your generated code, you have tounquoteit—which you have implicitly viabind_quoted, no problem!That means that the variable you next introduce,
type, is only available in the generated code. So when you proceed tounquote(type), you are getting the equivalent ofvariable type does not exists—since it does not exist inside the macro’s context.You have 3 options:
Unquote
optsand extract the type exclusively in the generated code context:Extract the type exclusively in the macro context and
unquotein the generate code:Extract the type exclusively in the macro context and
bind_quoted:I believe the 3rd option is popular, although personally I tend to prefer the second, as I like to be explicit about my
optsmunging andunquote-ing.shahryarjb
Thank you it is very good to know how to do.