Skip to main content

Your First Program

A guided tour of a complete mType program. We'll write a tiny todo-list app that exercises classes, generics, lambdas, the standard library, and string interpolation.

The Program

Save as todo.mt:

import * from "lib/collections/ArrayList.mt";

class Todo {
private string title;
private bool done;

public constructor(string title) {
this.title = title;
this.done = false;
}

public function complete(): void {
this.done = true;
}

public function toString(): string {
string mark = this.done ? "[x]" : "[ ]";
return $"{mark} {this.title}";
}
}

function main(): void {
ArrayList<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo("Read the docs"));
todos.add(new Todo("Write a program"));
todos.add(new Todo("Ship it"));

todos.get(0).complete();

for (Todo t : todos) {
print(t.toString());
}
}

main();

Run it:

mType todo.mt

Output:

[x] Read the docs
[ ] Write a program
[ ] Ship it

What This Demonstrates

FeatureWhere
Importsimport * from "lib/collections/ArrayList.mt"; — pulls a generic ArrayList<T> from the standard library.
Class declarationclass Todo { ... } — fields, constructor, methods.
Access modifiersprivate string title; and public constructor(...).
Generic instantiationnew ArrayList<Todo>() — explicit type argument.
String interpolation$"{mark} {this.title}"$"..." strings inline expressions.
Enhanced forfor (Todo t : todos) { ... } — iterate any Iterable<T>.
Standard libraryArrayList<T> from lib/collections/.

Next Steps