!CnJOKhVeFQDJJFlIls:57north.org.uk

Elixir

795 Members
Unofficial room for chatter about the Elixir language. This room is publicly logged.88 Servers

Load older messages


SenderMessageTime
24 Jan 2025
@terino:matrix.org@terino:matrix.orgRedacted or Malformed Event09:48:42
@philip:matrix.munksgaard.memunksgaard banned @terino:matrix.org@terino:matrix.org (spam).10:13:04
26 Jan 2025
@jayfivestacks:matrix.org@jayfivestacks:matrix.org joined the room.07:05:31
@tommy_12:matrix.org@tommy_12:matrix.org left the room.12:01:47
27 Jan 2025
@jayfivestacks:matrix.org@jayfivestacks:matrix.org left the room.06:17:54
@gianni:pals.chatGianni

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
@philip:matrix.munksgaard.memunksgaard
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:pals.chatGianniahh unfortunate, thank you19:08:38
@philip:matrix.munksgaard.memunksgaard
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-:matrix.orgNicd uhh isn't it @spec do_thing(value) :: value when value: val()? 19:21:07
@Nicd-:matrix.orgNicdhttps://hexdocs.pm/elixir/typespecs.html#defining-a-specification19:21:23
@philip:matrix.munksgaard.memunksgaard

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-:matrix.orgNicddialyzer is often very odd. I can write a clearly faulty function without that syntax and it won't complain19:38:51
@philip:matrix.munksgaard.memunksgaardI 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-:matrix.orgNicdI'm just reading the documentation and I recall seeing that syntax in use19:41:55
@philip:matrix.munksgaard.memunksgaardMy internal model of dialyzer has always been wrong. I can never quite predict how it'll work.19:42:36
@philip:matrix.munksgaard.memunksgaard

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
@philip:matrix.munksgaard.memunksgaard *

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
@philip:matrix.munksgaard.memunksgaard *

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:pals.chatGiannii would certainly expect dialyzer to blow up on that19:45:23
@gianni:pals.chatGiannibut it's always been hit or miss for me19:45:34
@Nicd-:matrix.orgNicderrors.jpg
Download errors.jpg
19:46:39
@philip:matrix.munksgaard.memunksgaard 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-:matrix.orgNicdI've read that dialyzer does actually ignore specs in some situations19:47:40
@Nicd-:matrix.orgNicdbut I can't say what those are19:47:44
@philip:matrix.munksgaard.memunksgaardRelated to types in Elixir, I just got this little teaser from Jose today: https://github.com/elixir-lang/ex_doc/issues/1819#issuecomment-261608833419:52:26
@Nicd-:matrix.orgNicdhopefully lets us get rid of deftypedstruct19:53:25
@Nicd-:matrix.orgNicdand the @whatever_keys19:53:47
28 Jan 2025
@tommy_13:matrix.orgBrandon joined the room.17:28:10
29 Jan 2025
@markus.h:matrix.org@markus.h:matrix.org left the room.16:50:19

Show newer messages


Back to Room ListRoom Version: