@@ -394,7 +394,7 @@ defmodule Protocol do
394
394
end
395
395
396
396
defp assert_impl! ( protocol , base , extra ) do
397
- impl = Module . concat ( protocol , base )
397
+ impl = __concat__ ( protocol , base )
398
398
399
399
try do
400
400
Code . ensure_compiled! ( impl )
@@ -684,14 +684,14 @@ defmodule Protocol do
684
684
types
685
685
|> List . delete ( Any )
686
686
|> Enum . map ( fn impl ->
687
- { [ Module.Types.Of . impl ( impl ) ] , Descr . atom ( [ Module . concat ( protocol , impl ) ] ) }
687
+ { [ Module.Types.Of . impl ( impl ) ] , Descr . atom ( [ __concat__ ( protocol , impl ) ] ) }
688
688
end )
689
689
690
690
{ domain , impl_for , impl_for! } =
691
691
case clauses do
692
692
[ ] ->
693
693
if Any in types do
694
- clauses = [ { [ Descr . term ( ) ] , Descr . atom ( [ Module . concat ( protocol , Any ) ] ) } ]
694
+ clauses = [ { [ Descr . term ( ) ] , Descr . atom ( [ __concat__ ( protocol , Any ) ] ) } ]
695
695
{ Descr . term ( ) , clauses , clauses }
696
696
else
697
697
{ Descr . none ( ) , [ { [ Descr . term ( ) ] , Descr . atom ( [ nil ] ) } ] ,
@@ -707,7 +707,9 @@ defmodule Protocol do
707
707
not_domain = Descr . negation ( domain )
708
708
709
709
if Any in types do
710
- clauses = clauses ++ [ { [ not_domain ] , Descr . atom ( [ Module . concat ( protocol , Any ) ] ) } ]
710
+ clauses =
711
+ clauses ++ [ { [ not_domain ] , Descr . atom ( [ __concat__ ( protocol , Any ) ] ) } ]
712
+
711
713
{ Descr . term ( ) , clauses , clauses }
712
714
else
713
715
{ domain , clauses ++ [ { [ not_domain ] , Descr . atom ( [ nil ] ) } ] , clauses }
@@ -746,7 +748,7 @@ defmodule Protocol do
746
748
end
747
749
748
750
defp change_impl_for ( { _name , _kind , meta , _clauses } , protocol , types ) do
749
- fallback = if Any in types , do: load_impl ( protocol , Any )
751
+ fallback = if Any in types , do: __concat__ ( protocol , Any )
750
752
line = meta [ :line ]
751
753
752
754
clauses =
@@ -762,7 +764,7 @@ defmodule Protocol do
762
764
end
763
765
764
766
defp change_struct_impl_for ( { _name , _kind , meta , _clauses } , protocol , types , structs ) do
765
- fallback = if Any in types , do: load_impl ( protocol , Any )
767
+ fallback = if Any in types , do: __concat__ ( protocol , Any )
766
768
clauses = for struct <- structs , do: each_struct_clause_for ( struct , protocol , meta )
767
769
clauses = clauses ++ [ fallback_clause_for ( fallback , protocol , meta ) ]
768
770
@@ -772,7 +774,7 @@ defmodule Protocol do
772
774
defp built_in_clause_for ( mod , guard , protocol , meta , line ) do
773
775
x = { :x , [ line: line , version: - 1 ] , __MODULE__ }
774
776
guard = quote ( line: line , do: :erlang . unquote ( guard ) ( unquote ( x ) ) )
775
- body = load_impl ( protocol , mod )
777
+ body = __concat__ ( protocol , mod )
776
778
{ meta , [ x ] , [ guard ] , body }
777
779
end
778
780
@@ -785,17 +787,13 @@ defmodule Protocol do
785
787
end
786
788
787
789
defp each_struct_clause_for ( struct , protocol , meta ) do
788
- { meta , [ struct ] , [ ] , load_impl ( protocol , struct ) }
790
+ { meta , [ struct ] , [ ] , __concat__ ( protocol , struct ) }
789
791
end
790
792
791
793
defp fallback_clause_for ( value , _protocol , meta ) do
792
794
{ meta , [ quote ( do: _ ) ] , [ ] , value }
793
795
end
794
796
795
- defp load_impl ( protocol , for ) do
796
- Module . concat ( protocol , for )
797
- end
798
-
799
797
# Finally compile the module and emit its bytecode.
800
798
defp compile ( definitions , signatures , { module_map , specs , docs_chunk } ) do
801
799
# Protocols in precompiled archives may not have signatures, so we default to an empty map.
@@ -957,7 +955,7 @@ defmodule Protocol do
957
955
# Define the implementation for built-ins
958
956
:lists . foreach (
959
957
fn { mod , guard } ->
960
- target = Module . concat ( __MODULE__ , mod )
958
+ target = Protocol . __concat__ ( __MODULE__ , mod )
961
959
962
960
Kernel . def impl_for ( data ) when :erlang . unquote ( guard ) ( data ) do
963
961
case Code . ensure_compiled ( unquote ( target ) ) do
@@ -1001,7 +999,7 @@ defmodule Protocol do
1001
999
1002
1000
# Internal handler for Structs
1003
1001
Kernel . defp struct_impl_for ( struct ) do
1004
- case Code . ensure_compiled ( Module . concat ( __MODULE__ , struct ) ) do
1002
+ case Code . ensure_compiled ( Protocol . __concat__ ( __MODULE__ , struct ) ) do
1005
1003
{ :module , module } -> module
1006
1004
{ :error , _ } -> unquote ( any_impl_for )
1007
1005
end
@@ -1074,7 +1072,7 @@ defmodule Protocol do
1074
1072
quote do
1075
1073
protocol = unquote ( protocol )
1076
1074
for = unquote ( for )
1077
- name = Module . concat ( protocol , for )
1075
+ name = Protocol . __concat__ ( protocol , for )
1078
1076
1079
1077
Protocol . assert_protocol! ( protocol )
1080
1078
Protocol . __impl__! ( protocol , for , __ENV__ )
@@ -1120,7 +1118,7 @@ defmodule Protocol do
1120
1118
else
1121
1119
# TODO: Deprecate this on Elixir v1.22+
1122
1120
assert_impl! ( protocol , Any , extra )
1123
- { Module . concat ( protocol , Any ) , [ for , Macro . struct! ( for , env ) , opts ] }
1121
+ { __concat__ ( protocol , Any ) , [ for , Macro . struct! ( for , env ) , opts ] }
1124
1122
end
1125
1123
1126
1124
# Clean up variables from eval context
@@ -1132,7 +1130,7 @@ defmodule Protocol do
1132
1130
else
1133
1131
__impl__! ( protocol , for , env )
1134
1132
assert_impl! ( protocol , Any , extra )
1135
- impl = Module . concat ( protocol , Any )
1133
+ impl = __concat__ ( protocol , Any )
1136
1134
1137
1135
funs =
1138
1136
for { fun , arity } <- protocol . __protocol__ ( :functions ) do
@@ -1157,7 +1155,7 @@ defmodule Protocol do
1157
1155
def __impl__ ( :for ) , do: unquote ( for )
1158
1156
end
1159
1157
1160
- Module . create ( Module . concat ( protocol , for ) , [ quoted | funs ] , Macro.Env . location ( env ) )
1158
+ Module . create ( __concat__ ( protocol , for ) , [ quoted | funs ] , Macro.Env . location ( env ) )
1161
1159
end
1162
1160
end )
1163
1161
end
@@ -1204,4 +1202,17 @@ defmodule Protocol do
1204
1202
{ Reference , :is_reference }
1205
1203
]
1206
1204
end
1205
+
1206
+ @ doc false
1207
+ def __concat__ ( left , right ) do
1208
+ String . to_atom (
1209
+ ensure_prefix ( Atom . to_string ( left ) ) <> "." <> remove_prefix ( Atom . to_string ( right ) )
1210
+ )
1211
+ end
1212
+
1213
+ defp ensure_prefix ( "Elixir." <> _ = left ) , do: left
1214
+ defp ensure_prefix ( left ) , do: "Elixir." <> left
1215
+
1216
+ defp remove_prefix ( "Elixir." <> right ) , do: right
1217
+ defp remove_prefix ( right ) , do: right
1207
1218
end
0 commit comments