24 Jan 2025 |
@terino:matrix.org | Redacted or Malformed Event | 09:48:42 |
| munksgaard banned @terino:matrix.org (spam). | 10:13:04 |
26 Jan 2025 |
| @jayfivestacks:matrix.org joined the room. | 07:05:31 |
| @tommy_12:matrix.org left the room. | 12:01:47 |
27 Jan 2025 |
| @jayfivestacks:matrix.org left the room. | 06:17:54 |
Gianni | is there a way to provide a single type signature that indicates that an argument to a function can be one of N values, but the return must be the same as what was passed in?
e.g.:
@type val() :: :ok | :great | :wonderful
@spec do_thing(value :: val()) :: value
def do_thing(value), do: value
| 19:07:35 |
munksgaard | In reply to @gianni:pals.chat
is there a way to provide a single type signature that indicates that an argument to a function can be one of N values, but the return must be the same as what was passed in?
e.g.:
@type val() :: :ok | :great | :wonderful
@spec do_thing(value :: val()) :: value
def do_thing(value), do: value
No, unfortunately that is not possible in Elixirs current type system. | 19:08:16 |
Gianni | ahh unfortunate, thank you | 19:08:38 |
munksgaard | In reply to @gianni:pals.chat
is there a way to provide a single type signature that indicates that an argument to a function can be one of N values, but the return must be the same as what was passed in?
e.g.:
@type val() :: :ok | :great | :wonderful
@spec do_thing(value :: val()) :: value
def do_thing(value), do: value
* No, unfortunately that is not possible in Elixir's current type system. | 19:12:53 |
Nicd | uhh isn't it @spec do_thing(value) :: value when value: val() ? | 19:21:07 |
Nicd | https://hexdocs.pm/elixir/typespecs.html#defining-a-specification | 19:21:23 |
munksgaard | I don't think that actually works. This doesn't produce a dialyzer warning for me at least:
@spec swap(val()) :: val()
def swap(:ok), do: :great
def swap(:great), do: :wonderful
def swap(:wonderful), do: :ok
@type val() :: :ok | :great | :wonderful
@spec do_thing(value) :: value when value: val()
def do_thing(value), do: swap(value)
def another_thing() do
case do_thing(:ok) do
:ok -> 1
:great -> 2
:wonderful -> 3
end
end
| 19:37:08 |
Nicd | dialyzer is often very odd. I can write a clearly faulty function without that syntax and it won't complain | 19:38:51 |
munksgaard | I agree that dialyzer is odd, but otherwise I'm not sure what you're saying. Are you saying that it does indeed work and that there is something wrong with my example, or that it doesn't work? | 19:40:29 |
Nicd | I'm just reading the documentation and I recall seeing that syntax in use | 19:41:55 |
munksgaard | My internal model of dialyzer has always been wrong. I can never quite predict how it'll work. | 19:42:36 |
munksgaard | Here's an even simpler example that doesn't work:
@type val() :: :ok | :great | :wonderful
@spec do_thing(value) :: value when value: val()
def do_thing(:ok), do: :great
def do_thing(:great), do: :wonderful
def do_thing(:wonderful), do: :ok
| 19:44:10 |
munksgaard | * Here's an even simpler example that produce a dialyzer error even though I think it should:
@type val() :: :ok | :great | :wonderful
@spec do_thing(value) :: value when value: val()
def do_thing(:ok), do: :great
def do_thing(:great), do: :wonderful
def do_thing(:wonderful), do: :ok
| 19:44:22 |
munksgaard | * Here's an even simpler example that does not produce a dialyzer error even though I think it should:
@type val() :: :ok | :great | :wonderful
@spec do_thing(value) :: value when value: val()
def do_thing(:ok), do: :great
def do_thing(:great), do: :wonderful
def do_thing(:wonderful), do: :ok
| 19:44:39 |
Gianni | i would certainly expect dialyzer to blow up on that | 19:45:23 |
Gianni | but it's always been hit or miss for me | 19:45:34 |
Nicd | ![errors.jpg](https://matrix.org/_matrix/media/r0/thumbnail/matrix.org/ntKrDXfoBraJcpTokGrmFANU?height=360&method=scale&width=360) Download errors.jpg | 19:46:39 |
munksgaard | I think part of what confuses me about dialyzer is that it uses the specs to typecheck the internals of a function, but seems to disregard them completely when typing function calls. | 19:47:17 |
Nicd | I've read that dialyzer does actually ignore specs in some situations | 19:47:40 |
Nicd | but I can't say what those are | 19:47:44 |
munksgaard | Related to types in Elixir, I just got this little teaser from Jose today: https://github.com/elixir-lang/ex_doc/issues/1819#issuecomment-2616088334 | 19:52:26 |
Nicd | hopefully lets us get rid of deftypedstruct | 19:53:25 |
Nicd | and the @whatever_keys | 19:53:47 |
28 Jan 2025 |
| Brandon joined the room. | 17:28:10 |
29 Jan 2025 |
| @markus.h:matrix.org left the room. | 16:50:19 |