What is the Erlang any() in Elixir?

what is the Erlang any() in Elixir ?

any() from: https://hexdocs.pm/elixir/typespecs.html#basic-types

2 Likes

Also any(), but many people also use term().

2 Likes

That means I can translate this Erlang code to Elixir:

{stop, any(), State}

to:

{:stop, any(), state}

I tried but not work.

Can you give us the whole context, speak all the relevant lines in your application?

As it stands there is too little information to help you.

1 Like
-spec init(Hostname :: inet:hostname(), SessionCount :: non_neg_integer(),
           Address :: inet:ip_address(), Options :: list()) -> {'ok', iodata(), #state{}} | {'stop', any(), iodata()}.

This is an example

The syntax for typespecs differ between erlang and elixir. Your erlang typespec:

-spec init(Hostname :: inet:hostname(), SessionCount :: non_neg_integer(),
           Address :: inet:ip_address(), Options :: list()) -> {'ok', iodata(), #state{}}

Could look like this in elixir (assuming you use a struct with a typespec for state instead of an erlang record):

@spec init(hostname :: :inet.hostname(), session_count :: non_neg_integer(),
           address :: :inet.ip_address(), options :: list()) :: {:ok, iodata(), State.t()}

Take a look at the docs.

2 Likes

That’s a spec, you can translate it to the following Elixir code:

@spec init(hostname :: :inet.hostname, session_count :: non_neg_integer(), address :: :inet.ip_address, options :: list) :: {:ok, iodata, Record.record(state)} | {:stop, any, iodata}

So what exactly do you mean by using “any()” doesn’t work?

2 Likes

Please forgot my stupid question. I mixed the type with value.
After reading doc, looks like any term can be erlang any().

That is why it is called any() :grin:

4 Likes