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

Tee
can someone please explain to me how Enum.reduce works with maps
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement