ngrilly

ngrilly

Inline typespecs?

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!

Most Liked

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.

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…

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.

Last Post!

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.

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
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14362 124
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement