markmark206

markmark206

Can has? `def admin?(user_id: String.t()) :: boolean do`

I am returning to an Elixir project (finally!) after spending some (too much;) time in Python, and I am now curious about Elixir’s typespec notation.

Here is an example of a function definition in Python and in Elixir, with type hints / typespecs:

Python:

def is_admin(user_name: str) -> bool:
    ....

Elixir:

  @spec admin?(String.t()) :: boolean()
  def admin?(user_name) do
...

To my – admittedly, recently Python’ed – eye, the Python version seems a bit more elegant (and easier to read and to write) than the Elixir version. (Also, this might be the first time I am saying those words;).

I wonder

  1. how others in the Elixir community feel about this,
  2. how Elixir typespec notation came into existence (erlang-inspired?), and
  3. if there is an opportunity here for making the language even more delightful and user-friendly.

Wondering what folks think about this, and thank you!

Most Liked

stevensonmt

stevensonmt

Timely post for me as I just spent a while annotating a bunch of functions with typespecs. I’m not sure it’s my preference overall but I like the separate vs inline specs in Elixir because of function overloading and pattern matching on function defs. So if you have a function that takes an integer and does some computation based on the value of that integer you don’t need to repeat the spec for each def.

@spec foo(integer()) :: String.t()
def foo(1), do: "one"
def foo(2), do: "two"
def foo(_), do: "I'm tired"
baldwindavid

baldwindavid

Mostly I just wish that Elixir were statically typed. Other than that, I’d probably prefer inline type annotations as opposed to separate function specifications.

The specification keywords seem mostly fine and unsurprising even if slightly more verbose than in some other languages.

Admittedly, String.t() is the one thing that seems a little awkward, but there is a reason it ended up that way and, as you might imagine, you’ll likely forget it bothers you at some point.

eksperimental

eksperimental

If I am not mistaken typespecs were a later addition to Erlang and the idea way to not to have to change the language syntax, that is way it all resides in a module attribute.

If you use the type string() (which means a charlist) you will get a warning, you can use binary() instead (@baldwindavid shared a link about this). But String.t() means it is UTF-8.

Last Post!

stefanchrobot

stefanchrobot

I have a strong preference for type annotations to not be inlined: they are useful, but most of the time add noise. For example, it’s pretty obvious what the type is for the first function argument because of how Elixir code is organized (dedicated modules and pipe-friendliness).

When I’m looking at the docs, I somewhat follow this order:

  • Function head,
  • Examples,
  • @spec
  • @doc,
  • @moduledoc.

Where Next?

Popular in Discussions Top

jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement