113 lines
2.7 KiB
Plaintext
113 lines
2.7 KiB
Plaintext
---
|
|
title: Classes
|
|
description: Unified data and behavior with Java-style organization and Rust-powered execution.
|
|
icon: Shapes
|
|
---
|
|
|
|
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 use semicolons and the `name Type` convention. Methods use `fn` and take `*self` as the first parameter for shared access.
|
|
|
|
```mist
|
|
pub class Logger {
|
|
prefix String;
|
|
|
|
pub fn info(*self, message *str) {
|
|
self.log(LogLevel::Info, message);
|
|
}
|
|
|
|
fn log(*self, level LogLevel, message *str) {
|
|
println!("{level} {} {}", self.prefix, message);
|
|
}
|
|
}
|
|
```
|
|
|
|
## The Constructor
|
|
|
|
Mist uses the explicit `constructor` keyword for initialization:
|
|
|
|
```mist
|
|
pub constructor() {
|
|
self.prefix = "default".to_string();
|
|
}
|
|
```
|
|
|
|
Constructors can take parameters:
|
|
|
|
```mist
|
|
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();
|
|
}
|
|
```
|
|
|
|
## Inheritance
|
|
|
|
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
|
|
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 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.
|