Skip to content

Instantly share code, notes, and snippets.

@davehowell
Last active June 20, 2025 15:27
Show Gist options
  • Select an option

  • Save davehowell/500f082f221fc9430032e1aea14c929a to your computer and use it in GitHub Desktop.

Select an option

Save davehowell/500f082f221fc9430032e1aea14c929a to your computer and use it in GitHub Desktop.
mojo

In Mojo, composition is achieved by including one or more structs as fields within another struct, rather than using inheritance (which Mojo does not support). This is similar to how composition works in Go and Rust.

Example: Composition in Mojo Suppose you have two structs, Position and Drawable, and you want to compose them into a Sprite struct:

struct Position:
    var x: Int
    var y: Int

struct Drawable:
    fn draw(self):
        print("Drawing at position")

struct Sprite:
    var position: Position
    var drawable: Drawable

    fn render(self):
        self.drawable.draw()
        print(f"Sprite at ({self.position.x}, {self.position.y})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment