1
votes

For example in this typespec for module String:

@spec validate(String.t) :: {:atom}

What does it mean? and how can I test it through iex?

** (UndefinedFunctionError) function String.t/0 is undefined or private
    (elixir) String.t()

Update

Looks like some people dont see the necessity of testing which is the type of something through iex

For the rest of us who are learning elixir we can do it like this:

iex(8)> t String
@type t() :: binary()
@type codepoint() :: t()
@type grapheme() :: t()
@type pattern() :: t() | [t()] | :binary.cp()
iex(9)> t(String)
@type t() :: binary()
@type codepoint() :: t()
@type grapheme() :: t()
@type pattern() :: t() | [t()] | :binary.cp()

Also for the .t

If you want to refer to the “string” type (the one operated on by functions in the String module), use String.t/0 type instead.

source https://hexdocs.pm/elixir/typespecs.html#notes

1
It's a type, you don't need to test anything here.NoDisplayName

1 Answers

3
votes

It's not a property. Basically it's a reference to “string” type which is being used by functions from Elixir's String module. To fully understand it please read more here.

One reason for using String.t() is that Elixir discourages the use of string(). A string() is a "charlist" in Elixir, while String.t() is a UTF-8 encoded binary.