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
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
Very good to know - I will keep that in mind.
FWIW I have resolved ALL of my warnings with rc.3 .
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









