markmark206

markmark206

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!

Showing Posts 1 to 8

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.

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"
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.

markmark206

markmark206 OP

This is a great example for why a separate typespec is useful, thank you.

mudasobwa

mudasobwa

Creator of Cure

You can always achieve the custom python-like (or whatever else-like) syntax with a bit of metaprogramming.

christhekeele

christhekeele

I’ve also found that typed function signatures in Python get noisy. And the default formatter… does not make that look elegant. I like the separate type signature in Elixir, even though that often gets a little noisy, but it’s miles better.

eksperimental

eksperimental

Not necessarily. You could define a function head, which sometimes is needed in Elixir and define it the way you do in Python had Elixir supported such feature. Actually it is needed in the example given by @stevensonmt, otherwise your documentation will look like foo(arg1)

So you define your function head like:

@spec foo(integer()) :: String.t()
def foo(integer)
def foo(1), do: "one"
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.
— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
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
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
nseaSeb
AcmeScript — Writing JS hooks as if I were still using Elixir I’ve been having fun building a little something over the last few days: Ac...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & 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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews