Updated docs to 0.3.1
This commit is contained in:
@@ -4,58 +4,67 @@ description: Defining shared behavior and contracts with Mist's signature ergono
|
||||
icon: Sparkles
|
||||
---
|
||||
|
||||
Traits in Mist define a set of methods that a type must implement, facilitating polymorphism and shared behavior. While they mirror the logic of Rust traits, they utilize Mist’s **type-first** declaration style for method signatures.
|
||||
Traits in Mist define a set of methods that a type must implement, facilitating polymorphism and shared behavior. Method signatures use the `fn` keyword with `*self` for the instance parameter.
|
||||
|
||||
## Defining a Trait
|
||||
|
||||
A trait definition lists method signatures that implementing types must satisfy. Like functions, these signatures place the return type before the method name.
|
||||
A trait lists method signatures using `fn`, with `*self` as the instance parameter and the return type after the parameter list.
|
||||
|
||||
```mist
|
||||
pub trait Drawable {
|
||||
void draw(self*);
|
||||
str* metadata(self*);
|
||||
fn draw(*self);
|
||||
fn metadata(*self) *str;
|
||||
}
|
||||
```
|
||||
|
||||
## Implementing a Trait
|
||||
|
||||
To implement a trait for a specific type, use the `impl` keyword followed by the trait name and the target type. This block must contain all required methods defined in the trait.
|
||||
Use `impl Trait for Type` to provide implementations:
|
||||
|
||||
```mist
|
||||
impl Drawable for Task {
|
||||
void draw(self*) {
|
||||
fn draw(*self) {
|
||||
println!("Drawing task: {}", self.name);
|
||||
}
|
||||
|
||||
str* metadata(self*) {
|
||||
return self.name;
|
||||
fn metadata(*self) *str {
|
||||
self.name
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Default Implementations
|
||||
|
||||
Traits can provide default behavior for methods. Types implementing the trait can choose to override these defaults or use the provided implementation.
|
||||
Traits can provide default behavior for methods that implementing types may override:
|
||||
|
||||
```mist
|
||||
pub trait Identifiable {
|
||||
u32 get_id(self*);
|
||||
fn get_id(*self) u32;
|
||||
|
||||
bool is_valid(self*) {
|
||||
return self.get_id() > 0;
|
||||
fn is_valid(*self) bool {
|
||||
self.get_id() > 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Super-traits
|
||||
|
||||
Traits can build upon other traits. If a trait requires another trait to be implemented first, use the colon `:` syntax.
|
||||
A trait can require another trait using the colon `:` syntax:
|
||||
|
||||
```mist
|
||||
pub trait Animated : Drawable {
|
||||
void animate(self*, f32 delta_time);
|
||||
pub trait Speak {
|
||||
fn speak(*self) String;
|
||||
}
|
||||
|
||||
pub trait Greet : Speak {
|
||||
fn greet(*self) String;
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Consistent Signatures**: Method signatures within traits follow the language-wide `return_type name(params)` convention.
|
||||
- **Explicit Context**: Methods use `self*` or `self mut*` as the first parameter to define how the instance is accessed, mapping directly to Rust's reference rules.
|
||||
- **Static Dispatch**: By default, Mist traits leverage Rust's zero-cost generics and monomorphization, ensuring high performance.
|
||||
- **Predictable Contracts**: Traits act as strict blueprints; the Mist compiler ensures every implementation perfectly matches the interface before generating the corresponding Rust code.
|
||||
- **`fn` Signatures**: Method signatures use `fn`, consistent with free functions.
|
||||
- **Explicit Context**: Methods use `*self` as the first parameter, mapping directly to Rust's reference rules.
|
||||
- **Default Methods**: Traits can provide default implementations.
|
||||
- **Super-traits**: Colon syntax for expressing trait requirements.
|
||||
- **Static Dispatch**: By default, Mist traits leverage Rust's zero-cost generics and monomorphization.
|
||||
|
||||
Reference in New Issue
Block a user