victorolinasc

victorolinasc

Typespec style guidelines

I’d like to propose a silly typespec discussion: what is your style guidelines for writing typespecs?

I mean, do you:

  • use parentheses (zero arity functions, types, etc)?
  • use private types?
  • add docs to your typespecs?

Browsing the “source of truth” (Elixir’s own code base) it seems types aren’t usually documented (for example here) and they don’t use parentheses except for external t() types and zero arity functions.

I haven’t seem libraries add docs to type definitions either. inch_ex does not cover this scenario also (I’m guessing by looking at Plug report).

Some types in the Erlang documentation have a comment (for example here), but usually people just don’t bother documenting it.

I know this is just a typical style discussion, but with a dialyzer task going in standard mix I’d like to know general opinion about this :slight_smile:

Most Liked Responses

blatyo

blatyo

Conduit Core Team

I do whatever the elixir formatter does.

Private types are to my knowledge, only really useful within opaque types. I don’t generally use them because most of my custom types are usually named versions of the basic types. When I define structs, I type them publicly because I think of that as public API. If I were unsure of how I wanted to structure a struct, I might choose to make it opaque in that situation, so that users would only use the module I provide to interact with it. In that case the structure of the struct would be encapsulated in the module’s functions. If I used types only in the definition of the struct, I’d make those private.

I’d say most of the time it’s pretty obvious what the type is from just the name or it’s underlying type. For example, I wouldn’t need an explanation for:

@type name :: String.t()
@type server :: pid()

That’s because that’s the way the elixir formatter formats them.

When someone suggested adding something related to typespecs to credo, I seem to remember rrrene mentioning he hadn’t looked much into typespecs yet. So, it’s not a big surprise. I also completely ignored them when I first started Elixir.

I think that’s a good example where the type is opaque, so you need to know how you can create one.

Where Next?

Popular in Discussions Top

Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
IVR
Hi all, I’ve seen a number of related threads in the past, but I’d still be very curious to hear an up-to-date opinion on this topic. I...
New
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
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New

We're in Beta

About us Mission Statement