# Binary literals as types

**URL:** <https://erlangforums.com/t/binary-literals-as-types/4011>\
**Category:** Questions / Help\
**Tags:** erlang, types, dialyzer, typespec\
**Created:** [September 19, 2024, 1:53pm UTC](https://erlangforums.com/t/binary-literals-as-types/4011 "2024-09-19T13:53:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmin](https://erlangforums.com/user_avatar/erlangforums.com/mmin/32/2193_2.png) [@mmin](https://erlangforums.com/u/mmin)\
**Post date:** [September 19, 2024, 1:53pm UTC](https://erlangforums.com/t/binary-literals-as-types/4011/1 "2024-09-19T13:53:25Z")

</div>

Why binary literals can’t be types so I could have something like:

```erlang
-type currency() :: <<"EUR">> | <<"USD">>.

```

?

Is there currently a way to express this?

---

<div class="post-metadata">

**Author:** ![TD5](https://erlangforums.com/user_avatar/erlangforums.com/td5/32/1590_2.png) [@TD5](https://erlangforums.com/u/TD5)\
**Post date:** [September 19, 2024, 2:01pm UTC](https://erlangforums.com/t/binary-literals-as-types/4011/2 "2024-09-19T14:01:43Z")

</div>

> Is there currently a way to express this?

I couldn’t tell you why it is how it is, but from the docs, it looks like what you want is entirely unrepresentable:

```erlang
Bitstring :: <<>>
           | <<_:M>> %% M is an Integer_Value that evaluates to a positive integer
           | <<_:_*N>> %% N is an Integer_Value that evaluates to a positive integer
           | <<_:M, _:_*N>>

```

_src: [Types and Function Specifications — Erlang System Documentation v27.0.1](https://www.erlang.org/doc/system/typespec.html)_

---

<div class="post-metadata">

**Author:** ![mmin](https://erlangforums.com/user_avatar/erlangforums.com/mmin/32/2193_2.png) [@mmin](https://erlangforums.com/u/mmin)\
**Post date:** [September 19, 2024, 2:06pm UTC](https://erlangforums.com/t/binary-literals-as-types/4011/3 "2024-09-19T14:06:24Z")

</div>

I know, but I wonder why is it like that.

I can image that could work very well with e.g. `gradualizer` and parsing libraries that operate on binaries (e.g. to check that all possible cases are handled).

---

<div class="post-metadata">

**Author:** ![williamthome](https://erlangforums.com/user_avatar/erlangforums.com/williamthome/32/471_2.png) [@williamthome](https://erlangforums.com/u/williamthome)\
**Post date:** [September 19, 2024, 4:40pm UTC](https://erlangforums.com/t/binary-literals-as-types/4011/4 "2024-09-19T16:40:37Z")

</div>

Here is some information regarding this “issue”: [Syntax error when using binary or string as a key in a map based type specification · Issue #6202 · erlang/otp · GitHub](https://github.com/erlang/otp/issues/6202).

---

<div class="post-metadata">

**Author:** ![aronisstav](https://erlangforums.com/user_avatar/erlangforums.com/aronisstav/32/270_2.png) [@aronisstav](https://erlangforums.com/u/aronisstav)\
**Post date:** [September 20, 2024, 8:48am UTC](https://erlangforums.com/t/binary-literals-as-types/4011/5 "2024-09-20T08:48:25Z")

</div>

This is a result of decisions made when designing the type system used by Dialyzer.

What can be represented in a type system must strike a balance between expressiveness and efficiency of operations. One can have opinions on what is “useful” and “consistent”. ~~As another example, string literals, while allowed in -specs and -types, get flattened by Dialyzer (i.e. “success” becomes “nonempty list of $s,$u,$c,$e”), but maybe/likely not by other tools.~~

---

<div class="post-metadata">

**Author:** ![mmin](https://erlangforums.com/user_avatar/erlangforums.com/mmin/32/2193_2.png) [@mmin](https://erlangforums.com/u/mmin)\
**Post date:** [September 20, 2024, 9:16am UTC](https://erlangforums.com/t/binary-literals-as-types/4011/6 "2024-09-20T09:16:31Z")

</div>

> [@aronisstav](#):
>
> string literals, while allowed

how do you mean string literals are allowed?

---

<div class="post-metadata">

**Author:** ![aronisstav](https://erlangforums.com/user_avatar/erlangforums.com/aronisstav/32/270_2.png) [@aronisstav](https://erlangforums.com/u/aronisstav)\
**Post date:** [September 20, 2024, 9:35am UTC](https://erlangforums.com/t/binary-literals-as-types/4011/7 "2024-09-20T09:35:28Z")

</div>

Sorry, I was wrong.

String literals like e.g. `-type one() :: "one".` are also not allowed. No idea why I thought they might have been.

---

<div class="post-metadata">

**Author:** ![TD5](https://erlangforums.com/user_avatar/erlangforums.com/td5/32/1590_2.png) [@TD5](https://erlangforums.com/u/TD5)\
**Post date:** [September 20, 2024, 1:43pm UTC](https://erlangforums.com/t/binary-literals-as-types/4011/8 "2024-09-20T13:43:09Z")

</div>

From Dialyzer’s perspective, there could be, since `erl_types:t_from_term/1` exists, and already handles syntax such as `"one"` for typing literals and patterns, and behaves as you said above. Maybe that’s what you were thinking of? I also had to double check this.
