@@ -21,31 +21,30 @@ defmodule Module.Types.Pattern do
21
21
do: { :ok , types , context }
22
22
end
23
23
24
- ## Patterns
25
-
26
24
@ doc """
27
- Return the type and typing context of a pattern expression
28
- with no {expected, expr} pair. of_pattern/4 must be preferred
29
- whenever possible as it adds more context to errors.
25
+ Return the type and typing context of a pattern expression with
26
+ the given {expected, expr} pair or an error in case of a typing conflict.
30
27
"""
31
- def of_pattern ( expr , stack , context ) do
28
+ def of_match ( expr , expected_expr , stack , context ) do
29
+ of_pattern ( expr , expected_expr , stack , context )
30
+ end
31
+
32
+ ## Patterns
33
+
34
+ defp of_pattern ( expr , stack , context ) do
32
35
# TODO: Remove the hardcoding of dynamic
36
+ # TODO: Possibly remove this function
33
37
of_pattern ( expr , { dynamic ( ) , expr } , stack , context )
34
38
end
35
39
36
- @ doc """
37
- Return the type and typing context of a pattern expression with
38
- the given {expected, expr} pair or an error in case of a typing conflict.
39
- """
40
-
41
40
# ^var
42
- def of_pattern ( { :^ , _meta , [ var ] } , expected_expr , stack , context ) do
41
+ defp of_pattern ( { :^ , _meta , [ var ] } , expected_expr , stack , context ) do
43
42
Of . intersect ( Of . var ( var , context ) , expected_expr , stack , context )
44
43
end
45
44
46
45
# left = right
47
46
# TODO: Track variables and handle nesting
48
- def of_pattern ( { := , _meta , [ left_expr , right_expr ] } , { expected , expr } , stack , context ) do
47
+ defp of_pattern ( { := , _meta , [ left_expr , right_expr ] } , { expected , expr } , stack , context ) do
49
48
case { is_var ( left_expr ) , is_var ( right_expr ) } do
50
49
{ true , false } ->
51
50
with { :ok , type , context } <- of_pattern ( right_expr , { expected , expr } , stack , context ) do
@@ -65,13 +64,13 @@ defmodule Module.Types.Pattern do
65
64
end
66
65
67
66
# %var{...} and %^var{...}
68
- def of_pattern (
69
- { :% , _meta , [ struct_var , { :%{} , _meta2 , args } ] } = expr ,
70
- expected_expr ,
71
- stack ,
72
- context
73
- )
74
- when not is_atom ( struct_var ) do
67
+ defp of_pattern (
68
+ { :% , _meta , [ struct_var , { :%{} , _meta2 , args } ] } = expr ,
69
+ expected_expr ,
70
+ stack ,
71
+ context
72
+ )
73
+ when not is_atom ( struct_var ) do
75
74
with { :ok , struct_type , context } <-
76
75
of_pattern ( struct_var , { atom ( ) , expr } , % { stack | refine: false } , context ) ,
77
76
{ :ok , map_type , context } <-
@@ -84,35 +83,35 @@ defmodule Module.Types.Pattern do
84
83
end
85
84
86
85
# %Struct{...}
87
- def of_pattern ( { :% , _meta , [ module , { :%{} , _ , args } ] } = expr , expected_expr , stack , context )
88
- when is_atom ( module ) do
86
+ defp of_pattern ( { :% , _meta , [ module , { :%{} , _ , args } ] } = expr , expected_expr , stack , context )
87
+ when is_atom ( module ) do
89
88
with { :ok , actual , context } <-
90
89
Of . struct ( expr , module , args , :merge_defaults , stack , context , & of_pattern / 3 ) do
91
90
Of . intersect ( actual , expected_expr , stack , context )
92
91
end
93
92
end
94
93
95
94
# %{...}
96
- def of_pattern ( { :%{} , _meta , args } , expected_expr , stack , context ) do
95
+ defp of_pattern ( { :%{} , _meta , args } , expected_expr , stack , context ) do
97
96
of_open_map ( args , [ ] , expected_expr , stack , context )
98
97
end
99
98
100
99
# <<...>>>
101
- def of_pattern ( { :<<>> , _meta , args } , _expected_expr , stack , context ) do
100
+ defp of_pattern ( { :<<>> , _meta , args } , _expected_expr , stack , context ) do
102
101
case Of . binary ( args , :pattern , stack , context , & of_pattern / 4 ) do
103
102
{ :ok , context } -> { :ok , binary ( ) , context }
104
103
{ :error , context } -> { :error , context }
105
104
end
106
105
end
107
106
108
107
# _
109
- def of_pattern ( { :_ , _meta , _var_context } , { expected , _expr } , _stack , context ) do
108
+ defp of_pattern ( { :_ , _meta , _var_context } , { expected , _expr } , _stack , context ) do
110
109
{ :ok , expected , context }
111
110
end
112
111
113
112
# var
114
- def of_pattern ( { name , meta , ctx } = var , { expected , expr } , stack , context )
115
- when is_atom ( name ) and is_atom ( ctx ) do
113
+ defp of_pattern ( { name , meta , ctx } = var , { expected , expr } , stack , context )
114
+ when is_atom ( name ) and is_atom ( ctx ) do
116
115
case stack do
117
116
% { refine: true } ->
118
117
Of . refine_var ( var , expected , expr , stack , context )
@@ -130,7 +129,7 @@ defmodule Module.Types.Pattern do
130
129
end
131
130
end
132
131
133
- def of_pattern ( expr , expected_expr , stack , context ) do
132
+ defp of_pattern ( expr , expected_expr , stack , context ) do
134
133
of_shared ( expr , expected_expr , stack , context , & of_pattern / 4 )
135
134
end
136
135
0 commit comments