Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation warning on Elixir 1.18 #492

Merged
merged 1 commit into from
Dec 25, 2024

Conversation

axelson
Copy link
Contributor

@axelson axelson commented Dec 24, 2024

Fix compilation warning for some schemas on Elixir 1.18. Fixes this warning:

    warning: comparison between distinct types found:

        Photos.PhotoSchema.__schema__(:type, capture) == :boolean

    given types:

        dynamic(:id or :naive_datetime_usec or nil or :string or :utc_datetime_usec) == :boolean

    where "capture" was given the type:

        # type: dynamic()
        # from: lib/photos/photos.ex:7
        capture

    While Elixir can compare across all types, you are comparing across types which are always disjoint, and the result is either always true or always false

    typing violation found at:
    │
  7 │   use Torch.Pagination,
    │   ~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/photos/photos.ex:7: Cute.Photos.paginate_photos/1

This occurred when my schema didn't have a boolean field:

defmodule Photos.PhotoSchema do
  use Ecto.Schema

  schema "photos" do
    field :name, :string
    field :path, :string
    field :comments, :string
    field :last_viewed_at, :naive_datetime_usec

    timestamps(type: :utc_datetime_usec)
  end
end

and using pagination like:

use Torch.Pagination,
  repo: Photos.Repo,
  model: Photos.PhotoSchema,
  name: :photos

I fix the error by indicating to Elixir that the code has been generated, which avoids this type error since depending on the module, the check will sometimes succeed (i.e. if you add a field :enabled, :boolean to the schema). I'm not sure about docs but I found this solution from:
https://elixirforum.com/t/elixir-v1-18-0-rc-0-released/68015/11?u=axelson

@cpjolicoeur cpjolicoeur merged commit b62629d into mojotech:master Dec 25, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants