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
Variables in Sharpy must be declared and assigned in a single statement. There are three syntactic forms:
3
+
Variables in Sharpy must be declared and assigned in a single statement. Variable declarations with type inference (Forms 2 and 3 below) are only allowed inside functions; module-level declarations require explicit type annotations.
4
+
5
+
There are three syntactic forms:
4
6
5
7
| Form | Syntax | Type Determination |
6
8
|------|--------|-------------------|
@@ -10,63 +12,80 @@ Variables in Sharpy must be declared and assigned in a single statement. There a
10
12
11
13
**Form 1: Explicit Type Annotation**
12
14
13
-
The type is explicitly specified:
15
+
The type is explicitly specified. This form can be used both at module level (static fields) and inside functions:
14
16
15
17
```python
16
-
count: int=0
17
-
name: str="Alice"
18
-
items: list[int] = [1, 2, 3]
19
-
user: User?=None
18
+
# Module level (static fields) - explicit type REQUIRED
19
+
counter: int=0
20
+
config: str="default"
21
+
22
+
defmain():
23
+
# Inside functions - explicit type allowed
24
+
count: int=0
25
+
name: str="Alice"
26
+
items: list[int] = [1, 2, 3]
27
+
user: User?=None
20
28
```
21
29
22
30
**Form 2: Type Inference (Implicit)**
23
31
24
-
The type is inferred from the initializer expression:
32
+
The type is inferred from the initializer expression. This form is **only allowed inside functions**, not at module level:
25
33
26
34
```python
27
-
count =0# Inferred as int
28
-
name ="Alice"# Inferred as str
29
-
items = [1, 2, 3] # Inferred as list[int]
30
-
pi =3.14159# Inferred as float
35
+
defmain():
36
+
count =0# Inferred as int
37
+
name ="Alice"# Inferred as str
38
+
items = [1, 2, 3] # Inferred as list[int]
39
+
pi =3.14159# Inferred as float
40
+
41
+
# ❌ NOT allowed at module level:
42
+
# count = 0 # ERROR: module-level requires type annotation
31
43
```
32
44
33
45
**Form 3: Type Inference (Explicit with `auto`)**
34
46
35
-
The `auto` keyword explicitly requests type inference. This is functionally equivalent to Form 2 but makes the inference explicit:
47
+
The `auto` keyword explicitly requests type inference. This is functionally equivalent to Form 2 but makes the inference explicit. Like Form 2, this is **only allowed inside functions**:
36
48
37
49
```python
38
-
count: auto =0# Inferred as int
39
-
name: auto ="Alice"# Inferred as str
40
-
items: auto = [1, 2, 3] # Inferred as list[int]
50
+
defmain():
51
+
count: auto =0# Inferred as int
52
+
name: auto ="Alice"# Inferred as str
53
+
items: auto = [1, 2, 3] # Inferred as list[int]
41
54
```
42
55
43
56
**When to Use `auto`:**
44
57
45
58
The `auto` keyword is primarily useful for variable shadowing, where you want to redeclare a variable with a different type:
46
59
47
60
```python
48
-
x: int=5
49
-
x =10# Assignment to existing int variable
50
-
x: str="hello"# Shadowing: new variable of type str
51
-
x: auto = [1, 2, 3] # Shadowing: new variable, type inferred as list[int]
61
+
defmain():
62
+
x: int=5
63
+
x =10# Assignment to existing int variable
64
+
x: str="hello"# Shadowing: new variable of type str
65
+
x: auto = [1, 2, 3] # Shadowing: new variable, type inferred as list[int]
52
66
```
53
67
54
68
## No Declaration Without Assignment
55
69
56
70
Unlike some languages, Sharpy does not allow variable declarations without initialization:
0 commit comments