Skip to content
Paolo Angeli edited this page Oct 13, 2019 · 5 revisions

Subroutine arguments

Var args

In Jai procedures and functions can have a variable number of arguments. The only limitation is that the other defined arguments must be called using their names.

concatenate_strings::( 
  astrings: ..string,                       // variable arguments 
  aseparator:= "",                          // argument with default value 
  aseparator_to_the_end := false            // argument with default value
) -> string {

  new_count = aseparator.count * (astrings.count - 1);             // sum the space rquired by separators between strings
  if aseparator_to_the_end new_count += aseparator.count;          // if we want a last separator increase the size

  for astrings new_count += it.count;                              // add the space required by all the strings

  r := alloc_string(new_count);                                    // reserve the memory for the new string
  c := cursor(r);                                                  // use a cursor to operate over the array 

  for astrings {
    c = append(c, it, r.count);                                    // append the string
    if it_index < (astrings.count - 1) || aseparator_to_the_end {  // if necessary
      c = append(c, aseparator, r.count);                          // append the separator
    }

  }

  return r;
}

player := "Sailor";
salutation := concatenate_strings("Hello", player, "How", "are", "you?", aseparator:= " ");   
// The previous function allocates memory so accordingly with the current context we should need to cleanup         
defer salutation;    

It is possible to use arrays to fill the data expected by varargs subroutines. This way it is not necessary to use named argument calls for the remaining ones.

powerup := "enchanted crystals";
message_items := (:string: player, "you are getting low on mana. Get some", powerup);                                                 
warning_message := concatenate(..message_items, " ");
defer warning_message ;   

It is possible to use varargs of type Any and then use RTTI to inspect the passed data and behave accordingly

transform_data( args: .. Any ){
 for args {
   if type_of(it) {
      case bool { ... }
      ...
   }  
 }
 
}

Types, constants and variables

  • Variables and assignments
  • Language data types
  • Simple user-defined data types
  • Expressions and operators
  • Type-casting
  • Pointers

Flow control

Procedures and functions

  • Declarations
  • Arguments / Parameters
  • Return values
  • Overloading / Polymorhism
  • Advanced features
  • Lambdas

Aggregated data types

  • Arrays
  • Strings
  • Composition of Structs

Advanced features

Clone this wiki locally