Default return values

Return values for functions in Jai may be defined with default values, similar to arguments. 1

fun :: (x: int) -> first: string = "Hello", second: string = "Sailor!" {
    if x == 0 return;
    if x == 1 return "Simple";
    if x == 2 return "Simple", "Simon";
    if x == 3 return second = "Simple", first = "Simon";

    return second = "Dolly"
}

a, b: string;

a, b = fun(0); // Hello Sailor!
a, b = fun(1); // Simple Sailor!
a, b = fun(2); // Simple Simon
a, b = fun(3); // Simon Simple
a, b = fun(4); // Hello Dolly

Named return values with defaults can provide a simpler alternative to exceptions: 2

Status :: enum {
  SUCCESS,
  NEGATIVE_NUMBERS_CONFUSE_MY_API,
  EVEN_NUMBERS_ARE_FOR_LOSERS
}

using Status.members;

Vector3 :: struct {
  x, y, z: float;
}

Entity :: struct {
  using position : Vector3;
  id : u32;
}

complicated_function :: (input : int) -> result: ^Entity = null, status := SUCCESS {
  if input < 0    return status = NEGATIVE_NUMBERS_CONFUSE_MY_API;
  if !(input % 2) return status = EVEN_NUMBERS_ARE_FOR_LOSERS;

  e := new Entity;
  e.id = cast(input, u32);

  return result = e;
}

  1. you can have default values on your return values, just like with arguments.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=2940 

  2. sometimes you would like a little bit of help managing the flow of information back up the stack of functions.
    “Arguments and Return Values” YouTube, uploaded by Jonathan Blow, Mar 10, 2015, https://youtu.be/CttIYXCUeVY?t=3076 

jailang 2019 pixeldroid
https://github.com/pixeldroid/jailang
programming pages theme v0.5.21 (https://github.com/pixeldroid/programming-pages)