Skip to content

Commit 2bfe0fe

Browse files
committed
Implement integer -> size(integer) syntax shortcut for bitsyntax
Closes #795
1 parent ded1747 commit 2bfe0fe

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
* enhancements
22
* [Binary] Support `<< "string" :: utf8 >>` as in Erlang
33
* [Binary] Support `\a` escape character in binaries
4+
* [Binary] Support syntax shortcut for specifying size in bit syntax
45
* [CLI] Support `--app` option to start an application and its dependencies
56
* [Dict] Support `put_new` in `Dict` and `Keyword`
67
* [Dict] Add `List.Dict` and a faster `HashDict` implementation

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ defmodule Kernel.SpecialForms do
154154
have their size specified explicitly). Bitstrings do not have
155155
a default size.
156156
157+
Size can also be specified using a syntax shortcut. Instead of
158+
writing `size(8)`, one can write just `8` and it will be interpreted
159+
as `size(8)`
160+
161+
<< 1 :: 3 >> == << 1 :: size(3) >> #=> true
162+
157163
The default unit for integers, floats, and bitstrings is 1. For
158164
binaries, it is 8.
159165

lib/elixir/src/elixir_literal.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ extract_bit_values(Meta, [{ Value, CallMeta, [_] = Args }|T] = All, Size, Types,
171171
extract_bit_values(Meta, T, NewSize, NewTypes, S)
172172
end;
173173

174+
extract_bit_values(Meta, [Size|T], default, Types, S) when is_integer(Size) andalso Size >= 0 ->
175+
extract_bit_values(Meta, T, {integer, Meta, Size}, Types, S);
176+
174177
extract_bit_values(Meta, [{ '|', _, [Left, Right] }], Size, Types, S) ->
175178
Expanded = 'Elixir.Macro':expand(Right, elixir_scope:to_ex_env({ ?line(Meta), S })),
176179
extract_bit_values(Meta, [Left|join_expansion(Expanded,[])], Size, Types, S);

lib/elixir/test/elixir/binary_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ bar
107107
sec_data :: binary >>
108108
end
109109

110+
test :bitsyntax_size_shorcut do
111+
assert << 1 :: 3 >> == << 1 :: size(3) >>
112+
assert << 1 :: [unit(8), 3] >> == << 1 :: [unit(8), size(3)] >>
113+
end
114+
110115
defmacrop signed_16 do
111116
quote do
112117
[big, signed, integer, unit(16)]

0 commit comments

Comments
 (0)