[erlang-questions] Emulating phantom types maybe

Tuncer Ayaz <>
Fri Aug 26 03:39:20 CEST 2016


On 26/08/2016, Michael Truog <> wrote:
> On 08/25/2016 03:32 PM, Tuncer Ayaz wrote:
> > What I'm looking for is a universal way to subtype
> > structurally-equivalent values to enforce that function needs_a/1
> > may only be fed input of type a/0 which may have been generated by
> > make_a() or make_other_a().
>
> In your original email, you said "tell Dialyzer that a function may
> only accept strings produced by a certain function" which may be a
> simpler way of describing the problem, if I understand it properly.
> Since Erlang type specifications can only have either atoms or
> integers as constants (unless you consider composite type sizes of
> tuples and binaries (binaries with bitstring syntax)) it is likely
> you want to use a constant to say "this type is coming from this
> function". So, a simple type can be something like "{function_a,
> Type}" to make sure the type knows it is coming from function_a.

I've considered explicitly tagging it in a tuple, like {ok,Val},
{a,Val}, and this is the conventional Erlang way. However, the idea
was that the values are structurally identical, and they're just the
result of certain functions only.

Thus, I was looking for a way to avoid the tuple, but maybe there is
no way to express this in type specs, unless you maybe pass around
fun objects to eval, which is a heavy weight solution.


More information about the erlang-questions mailing list