dogweather

dogweather

How to understand and read "t()"?

Here’s a screenshot of part of the Range API:

What does the Types section communicate?
To whom is it useful?
What is this syntax … Is it Elixir, Dialyzer, or both?
What meanings does t() have?
How is it related to .t often seen on modules?

I didn’t see answers to these in the docs … any pointers are appreciated.

Marked As Solved

NobbZ

NobbZ

That t() is the type t() in the Range module, or written otherwise Range.t()

Any member of the type %Range{first: integer(), last: integer()) is also a member of t().

The t(first, last) means, that you also can create a range from other types, you could for example spec a function to accept Range.t(float(), float()).

The syntax of the typelanguage is part of elixir as described in the Typespecs section of the elixir manual.

Types and typespecs are used in two ways:

  1. As documentation that humans know what kind of arguments a function can cope with. A reader of the spec disjoint?(Range.t(), Range.t()) :: boolean() knows that they need to pass in 2 ranges and not 2 integers or even an integer and a string. And also they know that this function will return a bool value, meaning either true or false.
  2. Dialyzer can use this information to tell us if we pass correct types into the functions we use.
14
Post #2

Also Liked

yawaramin

yawaramin

You can think of it as a type constructor. Type constructors can have different arities, like functions. They take parameters which are also types–i.e. type parameters. Think of it as generics.

ityonemo

ityonemo

Don’t think of them as normal functions, you can’t use them as normal functions during runtime. Almost every time, your type function will have arity zero; sometimes (almost never) you might need a parametric type function, in which case it can have an arity nonzero (usually one).

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement