Fix classes #13
This commit is contained in:
@@ -7,8 +7,9 @@ icon: Shapes
|
|||||||
Mist introduces `class` as syntactic sugar for a Rust struct with a virtual method table (vtable).
|
Mist introduces `class` as syntactic sugar for a Rust struct with a virtual method table (vtable).
|
||||||
|
|
||||||
```mist
|
```mist
|
||||||
pub class Animal {
|
pub class Animal
|
||||||
str& name,
|
{
|
||||||
|
str& name;
|
||||||
|
|
||||||
pub constructor(str& name)
|
pub constructor(str& name)
|
||||||
{
|
{
|
||||||
@@ -25,19 +26,21 @@ pub class Animal {
|
|||||||
Class fields can have default initializers:
|
Class fields can have default initializers:
|
||||||
|
|
||||||
```mist
|
```mist
|
||||||
class Player {
|
class Player
|
||||||
i32 health = 100,
|
{
|
||||||
str& name,
|
i32 health = 100;
|
||||||
|
str& name;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Inheritance
|
### Inheritance
|
||||||
|
|
||||||
```mist
|
```mist
|
||||||
class Dog : Animal {
|
class Dog : Animal
|
||||||
|
{
|
||||||
pub constructor(str& name)
|
pub constructor(str& name)
|
||||||
{
|
{
|
||||||
super(name);
|
super = Super::new(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void speak(&self) override
|
void speak(&self) override
|
||||||
@@ -56,6 +59,25 @@ void speak(&self) override(Animal)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Implementations
|
||||||
|
|
||||||
|
You can implement directly on the class body:
|
||||||
|
|
||||||
|
```mist
|
||||||
|
class Circle
|
||||||
|
{
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
|
impl std::fmt::Display
|
||||||
|
{
|
||||||
|
Result<(), std::fmt::Error> fmt(&self, Formatter<'_> f)
|
||||||
|
{
|
||||||
|
write!(f, "●")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Under the Hood
|
### Under the Hood
|
||||||
|
|
||||||
A class `Dog : Animal` generates:
|
A class `Dog : Animal` generates:
|
||||||
|
|||||||
Reference in New Issue
Block a user