-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_annotations.casa
More file actions
38 lines (36 loc) · 965 Bytes
/
type_annotations.casa
File metadata and controls
38 lines (36 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Type annotations on variable assignments
#
# The = name:type syntax allows explicit type annotation when assigning variables.
# This is useful for narrowing inferred or bare 'option' types to concrete types.
import "std"
# Basic type annotations
42 = x: int
"hello" = greeting: str
true = flag: bool
# Print annotated variables
x print "\n" print
greeting print "\n" print
flag print "\n" print
# Type annotations with option types
42 Option::Some = maybe_val: Option[int]
Option::None = empty_val: Option[int]
# Check option values
if maybe_val.is_some then
"maybe_val has value: " print
maybe_val.unwrap print "\n" print
fi
if empty_val.is_none then
"empty_val is none\n" print
fi
# Type annotations in functions
fn make_pair a:int b:int -> int int {
a = first: int
b = second: int
first second
}
10 20 make_pair = y: int = z: int
z print "\n" print
y print "\n" print
# Reassignment with annotated variable
100 = x
x print "\n" print