Updated docs to 0.3.1
This commit is contained in:
@@ -4,21 +4,21 @@ description: Unified data and behavior with Java-style organization and Rust-pow
|
||||
icon: Shapes
|
||||
---
|
||||
|
||||
Classes in Mist bridge the gap between Java's organizational structure and Rust's performance. They allow you to define data fields, constructors, instance methods, and trait implementations within a single, cohesive block.
|
||||
Classes in Mist bridge the gap between Java's organizational structure and Rust's performance. They group fields, constructors, and methods within a single cohesive block, using `fn` for methods and `*self` for the instance parameter.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
A class groups fields and methods together. Fields follow the `type name` convention, and methods define their logic directly within the class body.
|
||||
A class groups fields and methods together. Fields use semicolons and the `name Type` convention. Methods use `fn` and take `*self` as the first parameter for shared access.
|
||||
|
||||
```mist
|
||||
pub class Logger {
|
||||
String prefix;
|
||||
prefix String;
|
||||
|
||||
pub void info(self*, str* message) {
|
||||
pub fn info(*self, message *str) {
|
||||
self.log(LogLevel::Info, message);
|
||||
}
|
||||
|
||||
void log(self*, LogLevel level, str* message) {
|
||||
fn log(*self, level LogLevel, message *str) {
|
||||
println!("{level} {} {}", self.prefix, message);
|
||||
}
|
||||
}
|
||||
@@ -26,39 +26,87 @@ pub class Logger {
|
||||
|
||||
## The Constructor
|
||||
|
||||
Unlike languages that use the class name for initialization, Mist uses the explicit `constructor` keyword. This makes the entry point of the class unmistakable.
|
||||
Mist uses the explicit `constructor` keyword for initialization:
|
||||
|
||||
```mist
|
||||
pub constructor(str* prefix) {
|
||||
self.prefix = prefix.to_string();
|
||||
pub constructor() {
|
||||
self.prefix = "default".to_string();
|
||||
}
|
||||
```
|
||||
|
||||
## Instance Methods & `self`
|
||||
|
||||
Mist maintains Rust's explicit context handling. Any method that needs to access or modify class data must include `self*` (or `self mut*` for mutations) as its first parameter.
|
||||
Constructors can take parameters:
|
||||
|
||||
```mist
|
||||
pub void warning(self*, str* message) {
|
||||
pub constructor(prefix String) {
|
||||
self.prefix = prefix;
|
||||
}
|
||||
```
|
||||
|
||||
## Instance Methods & `*self`
|
||||
|
||||
Methods use `*self` (shared reference) or `*mut self` (mutable reference) as the first parameter. The return type is placed after the parameter list.
|
||||
|
||||
```mist
|
||||
pub fn warning(*self, message *str) {
|
||||
self.log(LogLevel::Warning, message);
|
||||
}
|
||||
|
||||
pub fn reset(*mut self) {
|
||||
self.prefix = String::new();
|
||||
}
|
||||
```
|
||||
|
||||
## Trait Implementations
|
||||
## Inheritance
|
||||
|
||||
One of Mist's most powerful features is the ability to nest trait implementations directly within the class block. This keeps the logic for how a type behaves (e.g., how it is displayed) physically coupled with the type definition.
|
||||
Classes support single inheritance with the `:` syntax. Use `super -> Super::new()` in the constructor to call the parent constructor. Override methods with `override` or `override(Parent)`.
|
||||
|
||||
```mist
|
||||
impl fmt::Display {
|
||||
std::fmt::Result fmt(self*, std::fmt::Formatter<'_> mut* f) {
|
||||
return write!(f, "logger ({})", self.prefix);
|
||||
pub class Animal {
|
||||
pub name String;
|
||||
|
||||
constructor() {
|
||||
self.name = "Rex".to_string();
|
||||
}
|
||||
|
||||
pub fn speak(*self) {
|
||||
println!("Unknown");
|
||||
}
|
||||
}
|
||||
|
||||
pub class Dog : Animal {
|
||||
constructor() {
|
||||
super -> Super::new();
|
||||
}
|
||||
|
||||
pub override fn speak(*self) {
|
||||
println!("Woof!");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Generics
|
||||
|
||||
Classes support generic type parameters:
|
||||
|
||||
```mist
|
||||
pub class Container<T> {
|
||||
pub value T;
|
||||
|
||||
constructor(val T) {
|
||||
self.value = val;
|
||||
}
|
||||
|
||||
pub fn get(*self) *T {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Unified Scope**: Data, behavior, and trait logic live in one place, eliminating the friction of jumping between `struct` and `impl` blocks.
|
||||
- **Explicit Context**: The use of `self*` ensures that the relationship between a method and its instance is always transparent.
|
||||
- **Encapsulation**: Visibility modifiers (`pub`) allow you to expose a clean API while keeping internal helper methods and state private to the class.
|
||||
- **Zero-Cost Classes**: Under the hood, Mist desugars these into idiomatic Rust structs and implementation blocks, ensuring no runtime overhead compared to raw Rust.
|
||||
- **Unified Scope**: Data and behavior live in one class block.
|
||||
- **`fn` Methods**: Methods use the `fn` keyword, consistent with free functions.
|
||||
- **`*self` Parameter**: The self reference is explicit and uses prefix `*` syntax.
|
||||
- **Inheritance**: Single inheritance with `override` for polymorphic dispatch.
|
||||
- **Encapsulation**: Visibility modifiers (`pub`) control API exposure.
|
||||
- **Zero-Cost Classes**: Under the hood, Mist desugars these into idiomatic Rust structs and implementation blocks.
|
||||
|
||||
@@ -1,57 +1,85 @@
|
||||
---
|
||||
title: Enums
|
||||
description: Defining algebraic data types with Mist's type-first convention.
|
||||
description: Defining algebraic data types with Mist's data-first convention.
|
||||
icon: Layers
|
||||
---
|
||||
|
||||
Enums in Mist serve as powerful algebraic data types (ADTs), maintaining the exact behavior and safety of Rust enums while applying the language-wide `type name` convention for variants that contain data.
|
||||
Enums in Mist serve as powerful algebraic data types (ADTs), maintaining the exact behavior and safety of Rust enums while using square brackets for tuple variant types and `name Type` for struct-like fields.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
An enum can contain unit variants, tuple variants, or struct-like variants. Following Mist's core philosophy, struct-like variants place the type before the identifier.
|
||||
An enum can contain unit variants, tuple variants (with types in square brackets), or struct-like variants.
|
||||
|
||||
```mist
|
||||
pub enum TaskState {
|
||||
Pending,
|
||||
InProgress,
|
||||
Completed,
|
||||
// Struct-like variant using 'type name'
|
||||
Failed {
|
||||
String reason,
|
||||
i32 code,
|
||||
reason String,
|
||||
code i32,
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## Variant Types
|
||||
|
||||
Mist supports all standard variant shapes, ensuring a 1:1 mapping to the underlying Rust execution model.
|
||||
Mist supports all standard variant shapes:
|
||||
|
||||
```mist
|
||||
enum Message {
|
||||
Quit, // Unit
|
||||
Move(i32, i32), // Tuple
|
||||
Write(String), // Tuple
|
||||
ChangeColor { // Struct-like
|
||||
u8 r, u8 g, u8 b,
|
||||
},
|
||||
enum OptionInt {
|
||||
None, // Unit
|
||||
Some[i32], // Tuple (square brackets)
|
||||
}
|
||||
|
||||
enum Shape {
|
||||
Circle { radius i32 }, // Struct-like
|
||||
Rect { w i32, h i32 },
|
||||
}
|
||||
```
|
||||
|
||||
### Instantiation & Matching
|
||||
|
||||
Tuple variants are created with parentheses and matched with brackets:
|
||||
|
||||
```mist
|
||||
let x = OptionInt::Some(42);
|
||||
|
||||
match (x) {
|
||||
OptionInt::None => { println!("none"); }
|
||||
OptionInt::Some[v] => { println!("{}", v); }
|
||||
}
|
||||
```
|
||||
|
||||
Struct variants use brace notation:
|
||||
|
||||
```mist
|
||||
let c = Shape::Circle { radius: 5 };
|
||||
|
||||
match (c) {
|
||||
Shape::Circle { radius } => { println!("{}", radius); }
|
||||
Shape::Rect { .. } => { /* ignore */ }
|
||||
}
|
||||
```
|
||||
|
||||
## Generics & Lifetimes
|
||||
|
||||
Just like structs and functions, enums declare generics and lifetimes in a unified block. This is particularly useful for defining custom Result or Option types that handle references.
|
||||
Enums declare generics and lifetimes in angle brackets after the name. Reference types use the `*` prefix.
|
||||
|
||||
```mist
|
||||
pub enum Validation<'a, T> {
|
||||
Valid(T),
|
||||
Invalid {
|
||||
str'a* message,
|
||||
u32 error_id,
|
||||
message *'a str,
|
||||
error_id u32,
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Consistent Member Declaration**: Struct-like variants maintain the `type name` order, ensuring that data modeling feels identical whether you are defining a top-level `struct` or an `enum` variant.
|
||||
- **Rust-Native ADTs**: Enums compile directly to Rust enums, allowing for exhaustive pattern matching and zero-cost abstraction.
|
||||
- **Shared Visibility**: The `pub` modifier at the enum level exports all variants for use in other modules, matching Rust's visibility rules for enums.
|
||||
- **Comma-Separated Members**: Fields within struct-like variants are separated by commas, mirroring the syntax used in standard Mist structs.
|
||||
- **Consistent Declaration**: Struct-like variants use the `name Type` order, consistent with Mist structs.
|
||||
- **Square Bracket Tuples**: Tuple variant types use `[]` brackets, distinct from function calls.
|
||||
- **Rust-Native ADTs**: Enums compile directly to Rust enums, allowing exhaustive pattern matching and zero-cost abstraction.
|
||||
- **Shared Visibility**: The `pub` modifier at the enum level exports all variants.
|
||||
- **Comma-Separated Members**: Fields within struct-like variants are separated by commas.
|
||||
|
||||
@@ -4,19 +4,19 @@ description: Defining execution blocks with C-style ergonomics and Rust-powered
|
||||
icon: SquareFunction
|
||||
---
|
||||
|
||||
Functions are the primary unit of execution in Mist. They prioritize a traditional declaration order, placing the return type before the identifier.
|
||||
Functions are the primary unit of execution in Mist, declared with the `fn` keyword. Parameters follow the `name Type` convention and the return type is placed after the parameter list.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
A standard function requires a return type, a name, and a body. Use the `void` keyword for functions that do not return a value.
|
||||
A standard function begins with `fn`, followed by its name, parameters, and an optional return type. The last expression in a block is implicitly returned.
|
||||
|
||||
```mist
|
||||
i32 add(i32 a, i32 b) {
|
||||
return a + b;
|
||||
fn add(a i32, b i32) i32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
void log_status(str* message) {
|
||||
println!("{}", message);
|
||||
fn greet() {
|
||||
println!("Hello!");
|
||||
}
|
||||
```
|
||||
|
||||
@@ -25,48 +25,48 @@ void log_status(str* message) {
|
||||
Functions are private to their module by default. The `pub` modifier exports the function for cross-module access.
|
||||
|
||||
```mist
|
||||
pub i32 get_version() {
|
||||
return 1;
|
||||
pub fn get_version() i32 {
|
||||
1
|
||||
}
|
||||
|
||||
pub(crate) i32 internal_use() {
|
||||
return 0;
|
||||
pub(crate) fn internal_use() i32 {
|
||||
0
|
||||
}
|
||||
```
|
||||
|
||||
## Mutable Parameters
|
||||
|
||||
Use `mut` to allow a function to modify its local binding of a value.
|
||||
Use `mut` on a parameter to allow reassignment within the function body.
|
||||
|
||||
```mist
|
||||
void update_score(i32 mut current_score, i32 bonus) {
|
||||
fn update_score(mut current_score i32, bonus i32) i32 {
|
||||
current_score = current_score + bonus;
|
||||
current_score
|
||||
}
|
||||
```
|
||||
|
||||
## Generics & Lifetimes
|
||||
|
||||
Mist integrates type abstraction and memory management into a single generic block. Lifetimes and type parameters share the `< >` bracket following the identifier.
|
||||
Generics and lifetimes are declared in angle brackets after the function name. Lifetimes are placed before the `*` in reference types.
|
||||
|
||||
```mist
|
||||
pub str'a* choose_longer<'a, T: Display>(str'a* s1, str'a* s2, T meta) {
|
||||
println!("Metadata: {}", meta);
|
||||
return if (s1.len() > s2.len()) { s1 } else { s2 };
|
||||
fn choose_longer<'a>(s1 *'a str, s2 *'a str) *'a str {
|
||||
if (s1.len() > s2.len()) { s1 } else { s2 }
|
||||
}
|
||||
```
|
||||
|
||||
## Closures
|
||||
|
||||
Closures are anonymous functions that can capture their environment. The return type before the pipe is optional — when omitted, the closure body uses curly braces:
|
||||
Closures are anonymous functions defined with the `fn` keyword followed by parameters, an optional return type, and a body or expression.
|
||||
|
||||
```mist
|
||||
var add = |i32 a, i32 b| { a + b };
|
||||
let add = fn(a, b) -> a + b;
|
||||
add(2, 3);
|
||||
|
||||
// With explicit return type
|
||||
Option<i32> |var v| { Some(v) }
|
||||
|
||||
// Without return type
|
||||
var greet = |str* name| { println!("Hello {}", name) };
|
||||
// With a block body
|
||||
let greet = fn(name *str) {
|
||||
println!("Hello {}", name);
|
||||
};
|
||||
```
|
||||
|
||||
## Attributes & Metadata
|
||||
@@ -75,14 +75,15 @@ Metadata is applied via the `#[attr]` syntax directly above the declaration.
|
||||
|
||||
```mist
|
||||
#[inline]
|
||||
pub bool is_active(u32 id) {
|
||||
return id > 0;
|
||||
pub fn is_active(id u32) bool {
|
||||
id > 0
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Scannable Signatures**: Return types first for rapid identification of a function's output.
|
||||
- **`fn` Keyword**: Every function starts with `fn`, making declarations instantly recognizable.
|
||||
- **Implicit Returns**: The final expression in a block is automatically returned.
|
||||
- **Unified Abstraction**: Lifetimes and type constraints are declared in one location.
|
||||
- **Closure Support**: Anonymous functions with optional return type annotations.
|
||||
- **Zero-Cost Mapping**: Every function maps directly to a Rust `fn`.
|
||||
|
||||
@@ -4,7 +4,7 @@ description: Organizing code across files with module declarations and path-base
|
||||
icon: FolderTree
|
||||
---
|
||||
|
||||
Mist organizes code through a file-system based module system with explicit path imports, similar to Rust but with a cleaner import syntax.
|
||||
Mist organizes code through a file-system based module system with explicit path imports.
|
||||
|
||||
## The Module System
|
||||
|
||||
@@ -12,7 +12,7 @@ Each `.mist` file in `src/` corresponds to a module. The module tree mirrors the
|
||||
|
||||
### File-Based Modules
|
||||
|
||||
The file `src/main.mist` is the crate root. Other files are discovered through `mod` declarations or by name — no explicit declaration is needed when a file exists at a matching path.
|
||||
The file `src/main.mist` is the crate root. Other files are discovered through `mod` declarations or by name.
|
||||
|
||||
### Declaring Submodules
|
||||
|
||||
@@ -24,16 +24,16 @@ mod database;
|
||||
|
||||
## Imports
|
||||
|
||||
Use the `use` keyword with angle brackets to bring items from other modules or external crates into scope.
|
||||
Use the `use` keyword with a path to bring items from other modules or external crates into scope.
|
||||
|
||||
```mist
|
||||
use <std::fs>;
|
||||
use <std::process>;
|
||||
use <std::path::Path>;
|
||||
use <std::collections::HashMap>;
|
||||
use std::fs;
|
||||
use std::process;
|
||||
use std::path::Path;
|
||||
use std::collections::HashMap;
|
||||
|
||||
// Import specific items
|
||||
use <my_module::Helper>;
|
||||
use my_module::Helper;
|
||||
```
|
||||
|
||||
### Visibility
|
||||
@@ -41,7 +41,7 @@ use <my_module::Helper>;
|
||||
Items can be re-exported with a visibility modifier on the import:
|
||||
|
||||
```mist
|
||||
pub use <internal::format>;
|
||||
pub use internal::format;
|
||||
```
|
||||
|
||||
## Sidefiles
|
||||
|
||||
@@ -1,37 +1,49 @@
|
||||
---
|
||||
title: Pointers & References
|
||||
description: Explicit memory access with C-style ergonomics and Rust-native safety.
|
||||
description: Explicit memory access with prefix pointer syntax and Rust-native safety.
|
||||
icon: MousePointer2
|
||||
---
|
||||
|
||||
Mist simplifies Rust’s reference system by using a pointer-style syntax. While the symbols look like C-style pointers, they adhere strictly to Rust’s ownership and borrowing rules.
|
||||
Mist uses a prefix `*` syntax for reference types. While the symbols look like C-style pointers, they adhere strictly to Rust's ownership and borrowing rules.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
References are defined by placing a `*` after the type. By default, pointers are immutable (shared). To allow modification of the underlying data, use the `mut*` modifier.
|
||||
Reference types are written with `*` before the type. Use `*mut` for mutable references. The `&` and `&mut` operators create references from values.
|
||||
|
||||
```mist
|
||||
void increment(i32 mut* value, i32* limit) {
|
||||
if (value < limit) {
|
||||
value = value + 1;
|
||||
let x i32 = 42;
|
||||
let r *i32 = &x;
|
||||
|
||||
let mut y = 42;
|
||||
let r *mut i32 = &mut y;
|
||||
*r = 100;
|
||||
```
|
||||
|
||||
In function parameters:
|
||||
|
||||
```mist
|
||||
fn increment(value *mut i32, limit *i32) {
|
||||
if (*value < *limit) {
|
||||
*value = *value + 1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Lifetimes
|
||||
|
||||
Lifetimes are attached directly to the type before the pointer symbol. This maintains a clean visual flow where the "type-contract" (identity, duration, and mutability) is read from left to right.
|
||||
Lifetimes are placed between `*` and the type, reading as "pointer with lifetime to type":
|
||||
|
||||
```mist
|
||||
pub struct Inspector<'a> {
|
||||
pub str'a* target,
|
||||
pub u32'a mut* counter,
|
||||
pub target *'a str,
|
||||
pub counter *'a mut u32,
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Explicit Intent:** The `mut*` syntax clearly distinguishes between a reference that can read and one that can write, mapping 1:1 to Rust's `&` and `&mut`.
|
||||
- **Visual Consistency:** Lifetimes (`'a`) and mutability modifiers are integrated into the type declaration, keeping function signatures and struct fields compact.
|
||||
- **Safety Guaranteed:** Despite the "pointer" appearance, the Mist compiler enforces Rust’s borrow checker. You cannot have multiple `mut*` references to the same data, and references cannot outlive their owners.
|
||||
- **Zero Overhead:** Mist pointers are "thin" or "fat" exactly like Rust references; they carry no extra runtime metadata and compile to identical machine code.
|
||||
- **Prefix Pointer Syntax**: `*Type` for shared references, `*mut Type` for mutable references.
|
||||
- **Explicit Intent**: The `*mut` syntax clearly distinguishes read-only from writable references, mapping 1:1 to Rust's `&` and `&mut`.
|
||||
- **Visual Consistency**: Lifetimes (`'a`) are placed before the type in `*'a Type`, keeping the declaration flow left-to-right.
|
||||
- **Safety Guaranteed**: Despite the "pointer" appearance, the Mist compiler enforces Rust's borrow checker.
|
||||
- **Zero Overhead**: Mist pointers compile to identical machine code as Rust references.
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
---
|
||||
title: Structs
|
||||
description: Data modeling using Mist's type-first convention.
|
||||
description: Data modeling with Mist's name-first field convention.
|
||||
icon: Form
|
||||
---
|
||||
|
||||
Structs in Mist follow the same structural logic as Rust, but apply the language-wide `type name` declaration style and allow for comma-separated field grouping.
|
||||
Structs in Mist follow the same structural logic as Rust, with fields using the language-wide `name Type` convention and comma-separated grouping.
|
||||
|
||||
## Basic Syntax
|
||||
|
||||
A struct is defined by its name followed by a block of fields. Each field follows the Mist convention of placing the type before the identifier, separated by commas.
|
||||
A struct is defined by its name followed by a block of fields. Each field places the identifier before the type.
|
||||
|
||||
```mist
|
||||
pub struct Task {
|
||||
pub String name,
|
||||
pub TaskState state,
|
||||
pub i32 executions,
|
||||
pub name String,
|
||||
pub state TaskState,
|
||||
pub executions i32,
|
||||
}
|
||||
```
|
||||
|
||||
@@ -24,8 +24,8 @@ Use the `pub` modifier to make the struct or its individual fields accessible fr
|
||||
|
||||
```mist
|
||||
pub struct NetworkNode {
|
||||
pub u32 id,
|
||||
str* address,
|
||||
pub id u32,
|
||||
address *str,
|
||||
}
|
||||
```
|
||||
|
||||
@@ -34,27 +34,36 @@ pub struct NetworkNode {
|
||||
Structs are instantiated using the standard brace syntax.
|
||||
|
||||
```mist
|
||||
var task = Task {
|
||||
let task = Task {
|
||||
name: "Initialize".to_string(),
|
||||
state: TaskState::Pending,
|
||||
executions: 0,
|
||||
};
|
||||
```
|
||||
|
||||
## Destructuring
|
||||
|
||||
Struct patterns use `let` with the struct name and field bindings:
|
||||
|
||||
```mist
|
||||
let p = Point { x: 3, y: 4 };
|
||||
let Point { x, y } = p;
|
||||
```
|
||||
|
||||
## Generics & Lifetimes
|
||||
|
||||
Generics and lifetimes are declared in angle brackets after the struct name. Lifetimes are associated with the reference/pointer type within the field declarations.
|
||||
Generics and lifetimes are declared in angle brackets after the struct name. Reference types use the `*` prefix.
|
||||
|
||||
```mist
|
||||
pub struct Buffer<'a, T> {
|
||||
pub T'a* data,
|
||||
pub usize len,
|
||||
pub data *'a T,
|
||||
pub len usize,
|
||||
}
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Type-First Declaration**: Fields use the `type name` order to match function parameters and variable declarations.
|
||||
- **Comma-Separated Members**: Fields are separated by commas, maintaining a clean and consistent delimiter style.
|
||||
- **Name-First Declaration**: Fields use `name Type` order, consistent with function parameters and variable declarations.
|
||||
- **Comma-Separated Members**: Fields are separated by commas, maintaining a clean delimiter style.
|
||||
- **Rust Compatibility**: Maps 1:1 to Rust structs, ensuring zero-cost abstraction and full ecosystem interoperability.
|
||||
- **Direct Visibility**: The `pub` modifier controls access at the struct and field level.
|
||||
|
||||
@@ -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