tjchambers

tjchambers

Type Checking macros with defoverrideable?

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.

First Post!

tjchambers

tjchambers

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.

Most Liked

tjchambers

tjchambers

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

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

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement