Operators, keywords, and types

Keywords and types

  • Keep only a single space between the type and the pipe operator when it is in an union type (e.g., string | int).

Example,

type method "POST" | "GET" | "PUT";

(int | string) variable = 0;

function getValue(string key) returns (string | error) {
    ...
}

function getName() returns string | error {
    (string | error) valueOrError = getValue("name");
    ...
}
  • Do not keep spaces between the type and the optional operator ?.

Example,

string? name;
  • Avoid line breaks in constrained types.

Do's

  map<int | string> // map reference type

Don'ts

map<
    int
    |
    string
> 

Operators

  • Keep only a single space before and after the = operator.

Example,

int a = 0;
  • Do not keep spaces around the semicolon ;.
  • Do not keep spaces between the unary operator and the expression.

Example,

a = -a;
  • Keep a single space before and after any binary or ternary operator.

Example,

var fullName = firstName + lastName;

string | () name = isNameAvailable() ? getName() : "Unknown";

var elvisOperator = name ?: "Unknown";
  • Keep a single space before and after a compound operator such as -= and +=.

Example,

name += lastName;
  • When accessing a function, object, or record from another module, do not keep spaces around :.

Example,

io:println("john");
http:Response res = new();