Skip to content

Commit dc47777

Browse files
committed
add static methods
1 parent 7368c5b commit dc47777

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/common-concepts/structures.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,33 @@ let pos = &Position{x: 10, y: 20}
126126
`pos` variable is the reference points to heap-allocated `Position` structure instance.
127127
::: warning
128128
If you not have any idea about references, check the [memory management documentations](/memory/memory-management).
129-
:::
129+
:::
130+
131+
132+
## Static Methods
133+
134+
Static methods, like normal methods, are dependent on the structure itself, but there are some differences.
135+
136+
These differences are:
137+
138+
- Can called via type declaration without instances
139+
- Don't dependent to instances
140+
- Don't takes receiver parameters
141+
- Can't access via instances
142+
143+
For example:
144+
145+
```
146+
struct Dog {}
147+
148+
impl Dog {
149+
static fn voice() {
150+
outln("woof woof")
151+
}
152+
}
153+
154+
fn main() {
155+
// Call static method via type declaration.
156+
Dog.voice()
157+
}
158+
```

0 commit comments

Comments
 (0)