-
Notifications
You must be signed in to change notification settings - Fork 36
Open
Labels
T:FeatureType: FeatureType: Feature
Description
It would be great if it were possible to leverage this library to also declare typed exceptions.
Exceptions are declared in the same way as Structs but using defexception instead of defstruct.
I can think of 2 ways to accomplish this:
- Use an option passed into the macro i.e.
typedstruct exception: true doand pass that down to the__typedstruct__call and replace the existing
defstruct @ts_fields
with
if unquote(!!opts[:exception]) do
defexception @ts_fields
else
defstruct @ts_fields
end
- Leverage the Plugin functionality.
Add a module attribute:
Module.put_attribute(__MODULE__, :def_impl, &defstruct/1)
and similarly to the above, replace the line
defstruct @ts_fields
with
@def_impl.(@ts_fields)
and then a plugin something like:
defmodule DefExceptionPlugin do
use TypedStruct.Plugin
@impl true
@spec init(keyword()) :: Macro.t()
defmacro init(_opts) do
quote do
Module.put_attribute(__MODULE__, :def_impl, &defexception/1)
end
end
end
I'm happy to raise a PR if you believe either of these are viable solutions.
Thanks
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
T:FeatureType: FeatureType: Feature
Projects
Status
Backlog