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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
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
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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

We're in Beta

About us Mission Statement