Skip to content

Latest commit

 

History

History
90 lines (68 loc) · 1.25 KB

File metadata and controls

90 lines (68 loc) · 1.25 KB

Functions (Jet » Design)

Overview

Definition

No return, no parameters:

fn main {
  println("Hello, {} from Jet!", "World");
}

No return value, with parameters:

fn print_sum(a: i32, b: i32) {
  println("{} + {} = {}", a, b, a + b);
}

With return value, no parameters:

fn ask_name -> String {
  ret prompt("What's your name?");
}

With return value, with parameters:

fn add(a: i32, b: i32) -> i32 {
  ret a + b;
}

Calling

Function declaration order is not taken into account:

fn main {
  let name = ask_name();
  println("Hello, {}!", name);
}

fn ask_name -> String {
  ret prompt("What's your name?");
}

Generic functions

Based on an arbitrary type:

fn add<T: type>(a: T, b: T) -> T {
  ret a + b;	
}

Based on an arbitrary constant:

fn sum<T: type, BASE: const>(a: T, b: T) -> T {
  ret a + b + BASE;
}

Based on an enum:

enum BookGenre {
  Fantasy,
  SciFi,
  Educational,
}

// `match` operation is prefixed with `$` because
// we're using a compile-time evaluation here.
fn get_book<B: BookGenre, U: type>() -> U {
  ret $match B {
	Fantasy => get_fantasy_book(),
	SciFi => get_scifi_book(),
	Educational => get_educational_book(),
  };
}