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
- how others in the Elixir community feel about this,
- how Elixir typespec notation came into existence (erlang-inspired?), and
- 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!
Trending in Discussions
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
Hey folks,
I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Hi everyone,
I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
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.
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 usebinary()instead (@baldwindavid shared a link about this). But String.t() means it is UTF-8.markmark206
This is a great example for why a separate typespec is useful, thank you.
mudasobwa
You can always achieve the custom python-like (or whatever else-like) syntax with a bit of metaprogramming.
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
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:
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:
@spec@doc,@moduledoc.