Skip to content

How to refactor for "Aliases could not be expanded for the given types" #99

@MrYawe

Description

@MrYawe

Since polymorphic_embed 3.0.7 I get this warning:

[warning] Aliases could not be expanded for the given types in [MY STRUCT].

This likely means the types are defined using a module attribute or another reference
that cannot be expanded at compile time. As a result, this may lead to unnecessary
compile-time dependencies, causing longer compilation times and unnecessary
re-compilation of modules (the parent defining the embedded types).

Ensure that the types are specified directly within the macro call to avoid these issues,
or refactor your code to eliminate references that cannot be expanded.

I understand that I must refactor my code to avoid unnecessary compile-time dependencies but I'm not sure how.

Here is how I use polymorphic_embed with a fictional example:

defmodule MyApp.Reminder do
  use Ecto.Schema
  import Ecto.Changeset
  import PolymorphicEmbed
  alias MyApp.Reminder.Channels

  schema "reminders" do
    polymorphic_embeds_one :channel,
      types: Channels.all(),
      on_type_not_found: :raise,
      on_replace: :update
  end
end

defmodule MyApp.Reminder.Channels do
  def all do
    [
      sms: MyApp.Channel.SMS,
      email: MyApp.Channel.Email
    ]
  end
end

The reason why we use a separate module to list all polymorphic_embed types is because we are using it in a lot of different schemas. Can I refactor my code while keeping a single source of truth with all types?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions