You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transform the struct update syntax into a type assertion
This transforms the struct update into a type assertion,
requiring the type system to be sure the expression has
precisely the given struct type.
The struct update syntax may still be deprecated in the
future but this will provide a safer migration path and
allow us to engage in more conversations with the community.
when defining the variable "x", you must also pattern match on "%Date{}".
997
+
998
+
hint: given pattern matching is enough to catch typing errors, you may optionally convert the struct update into a map update. For example, instead of:
999
+
1000
+
user = some_fun()
1001
+
%User{user | name: "John Doe"}
1002
+
1003
+
it is enough to write:
1004
+
1005
+
%User{} = user = some_fun()
1006
+
%{user | name: "John Doe"}
1007
+
"""
1008
+
1009
+
# When we don't know the type of capture
1010
+
asserttypeerror!([],&%Date{&1|day: 31})=~~l"""
1011
+
a struct for Date is expected on struct update:
1012
+
1013
+
%Date{&1 | day: 31}
1014
+
1015
+
but got type:
1016
+
1017
+
dynamic()
1018
+
1019
+
where "capture" was given the type:
1020
+
1021
+
# type: dynamic()
1022
+
# from: types_test.ex:LINE
1023
+
&1
1024
+
1025
+
instead of using &1, you must define an anonymous function, define a variable and pattern match on "%Date{}".
0 commit comments