Skip to main content

Mojo module

anytype

Defines the core traits for object lifetime management in Mojo.

This module provides the foundational traits that define how objects are created, managed and destroyed in Mojo:

  • AnyType: The most basic trait that all types extend by default. Types with this trait have no destructor and no lifetime management.

  • ImplicitlyDestructible: The base trait for types that require lifetime management through destructors. Any type that needs cleanup when it goes out of scope should implement this trait.

These traits are built into Mojo and do not need to be imported.

comptime values

Some

comptime Some[Trait: AnyTrait[AnyType]] = comptime[T: Trait] T

An alias allowing users to tersely express that a function argument is an instance of a type that implements a trait or trait composition.

For example, instead of writing

def foo[T: Intable, //](x: T) -> Int:
    return x.__int__()

one can write:

def foo(x: Some[Intable]) -> Int:
    return x.__int__()

Parameters

  • Trait (AnyTrait): The trait or trait composition that the argument type must implement.

Traits

Was this page helpful?