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
Copy file name to clipboardExpand all lines: content/More/15-distinct type type alias.md
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,21 +9,25 @@ weight: 15
9
9
- Uses the uniform `alias` syntax.
10
10
- Regular type aliases are the same as C `typedef`, C3 `typedef` creates a distinct type.
11
11
- Distinct types are type aliases that doesn't implicitly convert to the aliased type.
12
-
-`typedef inline` types can automatically convert to its underlying type and inherit the methods of its underlying type, but otherwise works as distinct types.
12
+
-`typedef inline` types can automatically convert to their underlying type and inherit the methods of their underlying type, but otherwise work as distinct types.
13
+
- By default constants of the underlying type have to be cast to a distinct type, but using the `@constinit` attribute will allow implicit conversion.
13
14
{{<end>}}
14
15
15
16
{{<defcod>}}
16
17
import std::io;
17
18
18
19
alias MyInt = int;
19
-
typedef MyId = int;
20
+
typedef MyId @constinit = int;
21
+
typedef StrongId = int;
20
22
21
23
fn void main()
22
24
{
23
25
MyInt x = 27;
24
26
MyId y = 3;
25
27
int a = x;
26
-
// int b = y; <- This doesn't work
28
+
StrongId b = (StrongId)42; // An explicit cast is required
0 commit comments