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

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31695 143
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
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 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement