tjchambers

tjchambers

Erlang: OTP 20
Elixir 1.20.0-rc.3 (9b80ab5) (compiled with Erlang/OTP 28)

I am wondering whether 1.20 of Elixir is supposed to support type checking macro’s that use defoverridable. In rc.3 I am getting dozens of type check warnings for a construction that may be a bit offbeat but serves my purpose. Before I go and try to construct the approach differently, I wanted to see if it is a gap in type checking, or whether the warnings take into account overridden functions.

The warnings are all of this style:

warning: the following clause will never match:

 _ ->

because it attempts to match on the result of:

 ends = scheduled_end(sc)

which has type:

 dynamic(nil)

type warning found at:
│
188 │           _ →
│           ~~~~
│
└─ lib/sct/tasks/services/task_helper.ex:188: Sct.Tasks.AddThemeCloudExclusionsTask.remaining_time/1

Code snippet:

defmodule Sct.Tasks.TaskHelper do

defmacro using(_) do
quote location: :keep do

  @doc """
  Returns the remaining time for the task
  """
  @spec remaining_time(StrategicCollaboration.t() | DateTime.t() | nil) :: String.t()
  def remaining_time(%StrategicCollaboration{active: true} = sc) do
    case ends = scheduled_end(sc) do
      nil ->
        ""
      _ ->
        remaining_time(ends)
    end
  end
      
  def remaining_time(nil), do: ""

  def remaining_time(%DateTime{} = ends) do
        about_when =
          SctWeb.Formatter.relative_time(ends)

        if about_when =~ "ago" do
          " - ended #{about_when}"
        else
          " - ends in #{about_when}"
          |> String.replace(" in in ", " in ")
          |> String.replace("ends in tomorrow", "ends tomorrow")
        end
      end

  @doc """
  Returns the scheduled end time for the task - stub for override
  """
  @spec scheduled_end(StrategicCollaboration.t()) :: DateTime.t() | nil
  def scheduled_end(%StrategicCollaboration{}), do: nil


  defoverridable(
    remaining_time: 1,
    scheduled_end: 1
  )
end

end
end

Example usage:

use Sct.Tasks.TaskHelper


“”"
Returns the scheduled end date for the task. The enddate is
read from the DB.
“”"
def scheduled_end(%StrategicCollaboration{active: true} = sc) do
  Sct.Schedule.Query.outcome_reasoning_schedule(sc).enddate
end




It appears that the type checking is being done on the macro definition which always returns nil for the scheduled_end and therefore sees anything but nil as a type mismatch in the case statement for remaining_time. Of course when the macro is used in a module where schedule_end is overridden it is possible that the overridden return value is a DateTime but sometimes when it has not been established in the DB it returns nil.

The construction I have for the 100+ tasks that use this TaskHelper module work fine, but the type-checking is flagging it as a warning. With 100’s of warnings it is a lot of noise.

I believe I may be able to re-construct this is some other manner, but wanted to bring it to one’s attention in case I am (likely) missing something.

Showing Posts 1 to 3

tjchambers

tjchambers OP

I have been able to revamp my macro use in a manner that makes this issue above academic. It may be useful (or not…) to have an explanation about how macros that have functions that are overridden is handled (or not handled) by the type system as it evolves.

But for me this is a non-issue.

josevalim

josevalim

Creator of Elixir

The type checker always sees the final version of the code. So if in the final version the function returns nil because it is not overridden, then that’s what is type checked. A quick solution here is to mark the code as generated:

quote generated: true do

But generally speaking, if you can reorganize the code to no longer emit dead code, that would be indeed preferred!

tjchambers

tjchambers OP

Very good to know - I will keep that in mind.

FWIW I have resolved ALL of my warnings with rc.3 .

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews