[erlang-questions] Emulating phantom types maybe

Michael Truog <>
Fri Aug 26 02:48:02 CEST 2016


On 08/25/2016 03:32 PM, Tuncer Ayaz wrote:
> On 26/08/2016, Leandro Ostera <> wrote:
>> Couldn't this be achieved by specifically defining a list of characters
>> that are lowercased?
>>
>> -type lowercase_char() :: "a" | "b" | ...
>> -type lowercase_string() :: [ lowercase_char() ]
> If we limit it to the string example, yes, but as I wrote, this
> isn't about strings exclusively.
>
> So, let's ignore lower/upper and think of something like raw vs
> sanitized string, if we stay with strings, although there can be
> cases where you could specify that constraint as well. Therefore,
> let's not focus on strings. Sorry for the bad example.
>
> 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.


More information about the erlang-questions mailing list