Equivalently, how can I typespec for a "single" UTF8 char?
Within a type definition, I can have generic "any string" or "any utf8 string" with
@type tile :: String.t # matches any string
@type tile :: <<_::8>> # matches any single byte
but it seems I can't match for the first bit to be 0
@type tile :: <<0::1, _::7>>
The case for single UTF bit sequence would be
@type tile :: <<0::1, _::7>> |
<<6::3, _::5, 2::2, _::6>> |
<<14::4, _::4, 2::2, _::6, 2::2, _::6>> |
<<30::5, _::3, 2::2, _::6, 2::2, _::6, 2::2, _::6>>
(these bit patterns match when using pattern matching, for instance
<<14::4, _::4, 2::2, _::6, 2::2, _::6>> = "○"
succeeds.)
But when used in typespecs, the compiler complains greatly with
== Compilation error in file lib/board.ex ==
** (ArgumentError) argument error
(elixir) lib/kernel/typespec.ex:1000: Kernel.Typespec.typespec/3
(elixir) lib/kernel/typespec.ex:1127: anonymous fn/4 in Kernel.Typespec.typespec/3
(elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/kernel/typespec.ex:1127: Kernel.Typespec.typespec/3
(elixir) lib/kernel/typespec.ex:828: anonymous fn/4 in Kernel.Typespec.typespec/3
(elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/kernel/typespec.ex:828: Kernel.Typespec.typespec/3
(elixir) lib/kernel/typespec.ex:470: Kernel.Typespec.translate_type/3
Is there any way to typespec to some bit pattern like this?
0..127::8
but I don't think it will work. – Maleenychar()
type spec would be closest to what you want but that still allows 0..255 (rather than just the 0..127 range). – Newton