Skip to content

Commit 4a0db2e

Browse files
author
José Valim
committed
Handle more types on bitsyntax.
1 parent 55aad68 commit 4a0db2e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ defmodule Kernel.SpecialForms do
9090
is an arbitrary series of bits. A binary is a special case of
9191
bitstring that has a total size divisible by 8.
9292
93-
The utf8, utf16, and utf32 types are for UTF code points.
93+
The utf8, utf16, and utf32 types are for UTF code points. They
94+
can also be applied to literal strings and char lists:
95+
96+
iex> <<"foo" :: utf16>>.
97+
<<0,102,0,111,0,111>>
9498
9599
The bits type is an alias for bitstring. The bytes type is an
96100
alias for binary.

lib/elixir/src/elixir_literal.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ types_allow_string([]) -> true;
122122
types_allow_string(_) -> false.
123123

124124
types_allow_splice(default) -> true;
125+
types_allow_splice([bytes]) -> true;
125126
types_allow_splice([binary]) -> true;
127+
types_allow_splice([bits]) -> true;
126128
types_allow_splice([bitstring]) -> true;
127129
types_allow_splice(_) -> false.
128130

lib/elixir/test/elixir/kernel/binary_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ bar
108108

109109
test :literal do
110110
assert <<106, 111, 115, 101>> == << "jose" :: binary >>
111+
assert <<106, 111, 115, 101>> == << "jose" :: bits >>
112+
assert <<106, 111, 115, 101>> == << "jose" :: bitstring >>
113+
assert <<106, 111, 115, 101>> == << "jose" :: bytes >>
111114

112115
assert <<106, 111, 115, 101>> == << "jose" :: utf8 >>
113116
assert <<106, 111, 115, 101>> == << 'jose' :: utf8 >>
@@ -122,6 +125,20 @@ bar
122125
assert <<0, 0, 0, 106, 0, 0, 0, 111, 0, 0, 0, 115, 0, 0, 0, 101>> == << 'jose' :: utf32 >>
123126
end
124127

128+
test :literal_errors do
129+
assert_raise ArgumentError, fn ->
130+
Code.eval_string(%b[<< "foo" :: integer >>])
131+
end
132+
133+
assert_raise ArgumentError, fn ->
134+
Code.eval_string(%b[<< "foo" :: float >>])
135+
end
136+
137+
assert_raise ArgumentError, fn ->
138+
Code.eval_string(%b[<< 'foo' :: binary >>])
139+
end
140+
end
141+
125142
@binary "new "
126143
@charlist 'old '
127144

0 commit comments

Comments
 (0)