Hey Paul,

I can't remove the comma from it thou, I get the following error, `use` 
macro expects 2 arguments as far as I can tell

```
** (ArgumentError) invalid arguments for use, expected a compile time atom 
or alias, got: Command[[aggregate_identifier: :id]]
    (elixir 1.11.2) lib/kernel.ex:5006: anonymous fn/3 in 
Kernel."MACRO-use"/3
    (elixir 1.11.2) lib/enum.ex:1399: Enum."-map/2-lists^map/1-0-"/2
    (elixir 1.11.2) expanding macro: Kernel.use/2
```

On Sunday, August 8, 2021 at 8:59:03 PM UTC-4 Paul Schoenfelder wrote:

> It looks to me like the error is due to a stray comma: `use Command, 
> [aggregate_identifier: :id] do` should be `use Command 
> [aggregate_identifier: :id] do`, which should work as you’d expect.
>
> As an aside, you might be interested in 
> https://github.com/bitwalker/strukt, which looks like it has a lot of 
> overlap with what you’re building (though what you are shooting for may be 
> totally different, I just see some similarities)
>
> Paul
>
> On Sun, Aug 8, 2021, at 7:54 PM, Adi wrote:
>
> I am not sure if supporting use/* is the right way forward here. The 
> `Kernel.use/2` macro is reserved for invoking a module's `__using__/1` 
> macro. To be honest, I don't like the fact that you're setting the module 
> attributes and defining the `embedded_schema` in the same macro. You can 
> easily just define the `@primary_key` in the `__using__/1` macro and 
> explicitly define the `embedded_schema` in `CreateRecurringTransfer` 
> itself, but that's my preference. 
>
> Either way, if you have defined a `__using__/3` macro, you can always 
> invoke it explicitly. Although I would rename it to avoid any confusion for 
> developers reading the code in the future and potentially thinking it has 
> something to do with `Kernel.use/2`.
>
> defmodule Command do
>   defmacro aggregate_schema(opts \\ [], do: block) do
>     unless Keyword.has_key?(opts, :aggregate_identifier) do
>       raise ArgumentError, "Missing :aggregate_identifier key"
>     end
>
>     aggregate_identifier = Keyword.fetch!(opts, :aggregate_identifier)
>
>     quote do
>       use Ecto.Schema
>       @primary_key { @aggregate_identifier_key, :binary_id, autogenerate: 
> true }
>       @derive Jason.Encoder
>        embedded_schema unquote(block)
>     end
>   end
> end
>
> defmodule CreateRecurringTransfer do
>   require Command
>   Command.aggregate_schema [aggregate_identifier: :id] do
>     field(:dtstart, :naive_datetime)
>     field(:dtend, :naive_datetime)
>     embeds_one(:amount, PositiveAmount)
>     embeds_one(:rrule, Rrule)
>     embeds_one(:owned_by, CustomerIdentity)
>     embeds_one(:from_account, TransferAccount)
>     embeds_one(:to_account, TransferAccount)
>   end
> end
>
> On Sunday, 8 August 2021 at 16:15:59 UTC-4 [email protected] wrote:
>
> I am trying to do the following,
>
> defmodule Command do
>   defmacro __using__(opts \\ [], do: block) do
>     unless Keyword.has_key?(opts, :aggregate_identifier) do
>       raise ArgumentError, "Missing :aggregate_identifier key"
>     end
>
>     aggregate_identifier = Keyword.fetch!(opts, :aggregate_identifier)
>
>     quote do
>       use Ecto.Schema
>       @primary_key { @aggregate_identifier_key, :binary_id, autogenerate: 
> true }
>       @derive Jason.Encoder
>        embedded_schema unquote(block)
>     end
>   end
> end
>
>
> Just so I can do something like,
>
> defmodule CreateRecurringTransfer do
>   use Command, [aggregate_identifier: :id] do
>     field(:dtstart, :naive_datetime)
>     field(:dtend, :naive_datetime)
>     embeds_one(:amount, PositiveAmount)
>     embeds_one(:rrule, Rrule)
>     embeds_one(:owned_by, CustomerIdentity)
>     embeds_one(:from_account, TransferAccount)
>     embeds_one(:to_account, TransferAccount)
>   end
> end
>
> But I get `** (CompileError) iex:3: undefined function use/3` which I 
> assume what I am trying to do is not supported.
>
> I am curious to understand why it doesn't work (beside maybe not being 
> supported), and what would be the problem by trying to support it
>
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/elixir-lang-core/46c25948-b96e-4982-b710-0ebeae1b5e96n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/elixir-lang-core/46c25948-b96e-4982-b710-0ebeae1b5e96n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/8f84974a-5464-496a-932f-e4d366f14e08n%40googlegroups.com.

Reply via email to