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,


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: https://hexdocs.pm/elixir/typespecs.html

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.

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

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

  defstruct [:x, :y, :z]

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

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.


Thank you :slight_smile: