Mukulch15

Mukulch15

I was going through the phoenix channel code and found the following typespec

 @type reply :: status :: atom | {status :: atom, response :: map}

I don’t understand the point of writing

:: status 

It would have made sense to me if it was only

@type reply :: atom | {status :: atom, response :: map}

Thanks

Showing Posts 1 to 4

Eiji

Eiji

@Mukulch15 Hmm, that’s interesting! … I also see it for first time.

Let’s assume that status as atom is :ok and response as map is %{}.

From what I understand after seeing code this gives us:

status = :ok
response = %{}
reply = status
# or
reply = {status, response}

That’s said I think that such code would be much more readable:

@type reply :: status | {status, response :: map()} when status: :atom 

However … it does not work …

** (CompileError) iex:2: invalid type specification: reply :: status | {status, response :: map()} when status: :atom
    (elixir 1.11.2) lib/kernel/typespec.ex:911: Kernel.Typespec.compile_error/2
    (stdlib 3.13.2) lists.erl:1267: :lists.foldl/3
    (elixir 1.11.2) lib/kernel/typespec.ex:226: Kernel.Typespec.translate_typespecs_for_module/2
    (elixir 1.11.2) src/elixir_erl_compiler.erl:12: anonymous fn/3 in :elixir_erl_compiler.spawn/2

I was really surprised seeing this, because I thought that’s supported when it’s actually not!

I have navigated to:

What I found surprising is that’s supported, but only in @spec (there are no equivalent examples for @type). This means we can write:

iex> defmodule Example do                                                    
...>   @spec reply() :: status | {status, response :: map()} when status: :atom
...>   def reply, do: :ok
...> end
{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 32, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 142,
   0, 0, 0, 15, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:reply, 0}}

Summary:

  1. It looks like @type have limited functionality compared to @spec i.e. it does not support guards.
  2. It looks like code you mentioned is using some kind of workaround for @type limitation.

Note: This is only my opinion after a quick analyze which may or may not be confirmed by it’s maintainers like @chrismccord.

rodrigues

rodrigues

It’s just about giving the type a name that can show up in docs etc, instead of atom, status :: atom explains what the value is.

I’m not very used to writing/seeing when in typespecs, @Eiji, but yeah I guess that looks more concise indeed :+1:

LostKobrakai

LostKobrakai

@spec does support the “when” notation because this is needed when the output type depends on the input type @spec something(x) :: x, but you also want to restrict the types at the same time: @spec something(x) :: x when x: atom.

Type declarations don’t need the the first part, so they don’t support the latter.

Mukulch15

Mukulch15 OP

Thanks for the detailed description. I was seeing @type reply :: status and :: atom | {status :: atom, response :: map} separately. Now it makes sense.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews