mirror of
https://github.com/vlang/v.git
synced 2025-09-15 02:18:47 -04:00
docs: add implements keyword for explicit interface implementations (#22214)
This commit is contained in:
parent
56013a443a
commit
888d84d26d
25
doc/docs.md
25
doc/docs.md
@ -3598,7 +3598,6 @@ fn main() {
|
|||||||
#### Implement an interface
|
#### Implement an interface
|
||||||
|
|
||||||
A type implements an interface by implementing its methods and fields.
|
A type implements an interface by implementing its methods and fields.
|
||||||
There is no explicit declaration of intent, no "implements" keyword.
|
|
||||||
|
|
||||||
An interface can have a `mut:` section. Implementing types will need
|
An interface can have a `mut:` section. Implementing types will need
|
||||||
to have a `mut` receiver, for methods declared in the `mut:` section
|
to have a `mut` receiver, for methods declared in the `mut:` section
|
||||||
@ -3645,6 +3644,30 @@ fn fn1(s Foo) {
|
|||||||
// }
|
// }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
There is an **optional** `implements` keyword for explicit declaration
|
||||||
|
of intent, which applies to `struct` declarations.
|
||||||
|
|
||||||
|
```v
|
||||||
|
struct PathError implements IError {
|
||||||
|
Error
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (err PathError) msg() string {
|
||||||
|
return 'Failed to open path: ${err.path}'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_open(path string) ! {
|
||||||
|
return PathError{
|
||||||
|
path: path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
try_open('/tmp') or { panic(err) }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
#### Casting an interface
|
#### Casting an interface
|
||||||
|
|
||||||
We can test the underlying type of an interface using dynamic cast operators.
|
We can test the underlying type of an interface using dynamic cast operators.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user