[Proposal] add "when" to @type and @typep syntax

25 views
Skip to first unread message

Rich Morin

unread,
Jul 7, 2019, 9:11:03 PM7/7/19
to elixir-lang-core
While editing a file of @type statements, it occurred to me that it would be
convenient to be able to abridge them like @spec statements. For example:

@type params :: %{ String.t => String.t }

could be rewritten as:

@type params :: %{ s => s } when s: String.t

Of course, this should also be possible with @typep statements.

Nu?

-r

Paul Schoenfelder

unread,
Jul 7, 2019, 10:49:43 PM7/7/19
to elixir-l...@googlegroups.com
This is mostly possible already, like so:

@type params(k, v) :: %{k => v}
@spec handle_params(params(s, s)) :: ok when s :: String.t

While Erlang doesn't support expressing guards on the generic parameters in a type definition, Elixir could just convert such definitions to the Erlang compatible form by replacing the type variables with the concrete type. In general though I tend to deal with the verbosity problem by extracting the noisy type name into its own definition that is more easily used, e.g.:

@type param_key :: String.t
@type param_val :: String.t
@type params :: %{param_key => param_val}

This has the advantage of being easier to read and easier to change in the future (the `params` type doesn't need to change if the range of types for keys/values changes).

Paul
-- 
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages