ngrilly

ngrilly

What is the reason for using a separate declaration for typespecs:

@spec round(number) :: integer
def round(x), do: # implementation...

Instead of “inlining” it, in a way similar to TypeScript, Flow or MyPy, with something like that:

def round(x: number) :: integer, do: # implementation...

?

Thanks for your insights!

Showing Posts 1 to 10

hubertlepicki

hubertlepicki

Typespecs in the form currently used in Elixir are not adding anything new to the language, nothing at all. They are using already existing feature of module attributes, i..e. the stuff you use with @ sign.

You can always add new syntax to the language itself, but if you do so it’s harder to remove it afterwards. Typespecs are now just data, and are treated as such, if they are ignored or changed altogether your programs will not break at all because they do not really affect your runtime.

I appreciate Elixir creator(s) keeping the language (fairly) small. Smaller is better.

ngrilly

ngrilly OP

My bad. I should have guessed, since I saw the @ sign used for other purposes as well. Thanks for clarifying this.

In TypeScript/Flow/MyPy, typespecs are just data, and are absolutely not used at runtime. The parts after the : or :: signs is just data attached to the function signature.

NobbZ

NobbZ

Types and specs in Elixir are not even used at compiletime, except that they get integrated into the compiled modules.

Then the can get evaluated using a tool called dialyzer, which is from the erlang/beam toolchain and not related to elixir (directly).

Since elixirc does not resolve types, but only stores the information of them into the module, it would telling some false story when they are inlined. As it is now, it is more or less obvious that they are an optional extra.

Also elixirs (and erlangs) type annotations are in a way powerfull that I can’t relly think a way of to express it inline while maintaining readability.

Consider this one (this is a single type and not 4!):

@spec foo(:error) :: 1
@spec foo(:two) :: "two"
@spec foo(integer) :: String.t
@spec foo(list(integer)) :: integer

Or the more classical ones;

@spec map(list(a), fun((a) -> b)) :: list(b) where a: var, b: var
@spec fold(list(a), b, fun((a, b) -> b) :: b where a: var, b: var

These specs are from the deepest areas of my mind, maybe I confused a bit syntax of them with erlang, but roughly they should show what I mean.

There are even specs much more complex you have to think about, just take a look at the contracts for all the GenServer like behaviours…

ngrilly

ngrilly OP

In JavaScript with Flow and Python 3 with MyPy, types are “inlined” but are not used by JavaScript or Python themselves. They are only used by the type checker (Flow for JavaScript and MyPy for Python 3). I don’t feel like it’s telling a “false story”. Users don’t seem confused by that approach.

But I can sympathize with your argument about readibility.

NobbZ

NobbZ

I don’t use Python or JavaScript, so I don’t know anything about that. But can you provide any examples that are as complex as my examples above?

Dialyzer types are not dependent ones, but the fact that you can specify an explicit set of return values without bothering with creating complete new types and conversion routines from that types into ordinary numbers (just as an example).

rvirding

rvirding

Creator of Erlang

It would make using pattern matching very cumbersome and the patterns would quickly become unreadable if they were to include type information.

mkunikow

mkunikow

I think I prefer separate declaration over inline. In my opinion is much cleaner.

For example in Haskell:

divideThenAdd :: Num a => a → a → a
divideThenAdd x y = (x / y) + 1

In Elm

distance : { x : Float, y : Float } → Float
distance {x,y} = sqrt (x^2 + y^2)

Also in typescript you can have separate ts files only with type declaration.

ngrilly

ngrilly OP

You’re right about pattern matching. Python and JavaScript don’t support matching. It’s probably the main reason why it’s possible to “inline” type specifications. In Elixir, it would awkward to “repeat” the type specification in each pattern. Thanks for answering.

kaashyapan

kaashyapan

Has there been any discussion/proposal over creating spec/typespecs in seperate files ?

Similar to Fsharp signature files or typescript declaration files.
https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/signature-files

I ask cos I find the type signatures in elixir tend to be more noisy.

Qqwy

Qqwy

TypeCheck Core Team

I do not think there has. The main disadvantage of moving specs to a different file is that it is very easy to forget them (and then they for instance are not updated when the functions themselves are changed).

In a language that performs full compile-time type checking it is enforced that the types and the actual signatures match, but in Elixir this is not the case.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4663 61218 595
New
cblavier
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...
515 12931 101
New
sntran
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
gtcode
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
AstonJ
A remark @Garrison made… ..has already inspired a thread for the technical side of reclaiming our internet, but there is of course anot...
New
ManuelGarcia
Hi everyone! I’m the developer of PaperForge, and I’ve been working on something alongside it that I’d like your opinion on. I’m conside...
New
bartblast
This is where the projects in the Hologram Showcase get posted: apps built with Hologram and libraries made for it. New ones go up usuall...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
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
OndrejValenta
(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
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews