dantyler

dantyler

What exactly is t() (except type specification)

Dear Elixir hobbyists and professionals,

I understand that t() is used for types. For example I can defstruct id: 0 in MyModule and specify @type t :: %__MODULE__{id: integer) and then from a different module I can use it as MyModule.t() as type.

I’m interested to understand what exactly in Elixir/Erlang the t() is.

In IEX I tried h String.t, h String.t/0 and i String.t but it doesn’t work so I’m assuming t() is not a function even it looks like a function.

If t() is not a function or atom, could you please tell me what is it?

Is OK to create t() function or define struct field as t - defstruct [:t]? I’m not saying I want to do that, it would be just interesting to understand if I could do that ;-).

Thank you!

Best Regards,

Dan

Most Liked

dimitarvp

dimitarvp

t() is basically a convention. You can just as easily have this module:

defmodule MyStruct do
  @type my_struct :: %__MODULE__{}

  defstruct [:x, :y, :z]
end

And then reference the type in foreign typespecs (= typespecs in different modules) like so:

def ModuleUsingMyStruct do
  @spec use_the_struct(MyStruct.my_struct()) :: map()
  def use_the_struct(%MyStruct{} = x) do
    Map.from_struct(x)
  end
end

You can see for yourself that this introduces some annoying extra typing so a convention emerged where t() is being used, namespaced within the containing module (example from standard lib: String.t()).

Yes, you can.

LostKobrakai

LostKobrakai

It’s a way to reference types. It might look like a function call, but everything after @type, @spec, … deals with types and types only. It’s not going become executable code.
For a more complete explanation take a look at this one: Typespecs reference — Elixir v1.20.2

dantyler

dantyler

Thank you LostKobrakai. I read typespecs on hexdocs before I asked. I was just thinking that there is underlying structure. For example String (if you just type it to IEX) is actually an atom, Struct is a map with extra fields…

If there is no underlying structure (like it’s not a function) it means that I can actually create a function t() or struct field “t” without braking something.

Last Post!

dantyler

dantyler

Thank you :slight_smile:

Where Next?

Popular in Questions Top

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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
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
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
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
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

We're in Beta

About us Mission Statement