Built-in Functions

Several functions are built into the WebL programming language (in contrast to functions written by the programmer). We distinguish between normal built-ins and special built-ins. Normal built-ins evaluate all their actual arguments before invoking the function. Special built-ins defer the evaluation of their arguments to the function being invoked. Examples of special built-ins include Time, Timeout, and Retry.

Most built-ins accept only a fixed number of arguments. Some built-ins like PrintLn accept any number of arguments of any value type. Variable length argument builtins are specified with ellipses (...) in See Core Built-in Functions. An actual argument can be of any value type if no explicit type is given in the table. The pseudotype any denotes values of any type.

As a shorthand we sometimes use the notation

argname: {type1, type2, ... }

 

to indicate that argname can be of type1 or type2. (See See Functions to Retrieve Web Pages.)

 

Core Built-in Functions

Function

Description

Assert(x: bool)

Throws an assertion failed exception if x is false.

Boolp(x): bool
Charp(x): bool
Funp(x): bool
Intp(x): bool
Listp(x): bool
Methp(x): bool
Objectp(x): bool
Realp(x): bool
Setp(x): bool
Stringp(x): bool
Pagep(x): bool
Piecep(x): bool
Tagp(x): bool
Piecesetp(x): bool

Predicates that check if a value is of a specific type.

Call(cmd: string): string

Executes a shell command and returns the output written to standard out while the command is running. The command string may contain references to variables in lexical scope by writing $var or ${var}. The value of these referenced variables are expanded before the command is executed.

Clone(o: object, p: object, ...): object

Makes a new object by copying all the fields of the objects passed as arguments. Fields of p have precedence over fields of o (and so on). The field ordering of the resulting object is defined by enumerating the fields of o, p, and so on in that sequence.

Error(x, y, z, ...): nil

Prints arguments to standard error output.

ErrorLn(x, y, z, ...): nil

Prints arguments to standard error output followed by end-of-line.

Eval(s: string): any

Evaluates the WebL program coded in string s.

Exec(cmd: string): int

Executes a shell command and returns the exit code returned by the command. The command string may contain references to variables in lexical scope by writing $var or ${var}. The value of these referenced variables are expanded before the command is executed.

Exit(errorcode: int)

Terminates the program with an error code.

DeleteField(o: object, fld): nil

Removes the field fld from the object o.

First(l: list): any

Returns the first element in a list.

GC(): nil

Explicitly invokes the Java garbage collector.

Native(classname: string): fun

Loads a WebL function1 implemented in Java.

Print(x, y, z, ...): nil

Prints arguments to standard output.

PrintLn(x, y, z, ...): nil

Prints arguments to standard output followed by end-of-line.

ReadLn(): string

Reads a line from standard input (throws away the end-of-line character).

Rest(l: list): list

Returns a list of all list elements except the first element.

Retry(x): any

Executes expression x and returns its value. In case x throws an exception, x is re-executed as many times as needed until it is successful.

Select(l: list, from: int, to: int): list

Extracts a sublist of l starting at element number from and ending at element number to (exclusive).

Select(s: string, from: int, to: int): string

Extracts a substring of starting at character number from and ending at character number to (exclusive).

 

Select(s: set, f: fun): set
Select(l: list, f: fun): list
Select(p: pieceset: f: fun): pieceset

Maps sets, lists, and piecesets to sets, lists, and piecesets respectively according to a membership function f. Function f must have a single argument and must return a boolean value indicating whether the actual argument is to be included in the set, list or pieceset.

Sign(x: int): int
Sign(x: real): int

Returns -1, 0, +1 if x < 0, x = 0, and x > 0 respectively.

Size(l: list): int

Returns the number of elements in a list.

Size(s: set): int

Returns the number of elements in a set.

Size(s: string): int

Returns the number of characters in a string.

Sleep(ms: int): nil

Suspends thread execution for the specified number of milliseconds.

Sort(l: list, f: fun): list

Sorts the elements of l according to the comparison function f. The function f needs to take two formal arguments and return -1, 0, or +1 if the actual arguments are less, equal, or more than each other.

Stall()

Program goes to sleep forever.

Throw(o: object)

Generates an exception.

Time(x): int

Returns the time (in milliseconds) it takes to evaluate the expression x.

Timeout(ms: int, x): any

Performs the expression x and returns its value. If the evaluation takes more than the specified amount of time (in milliseconds), an exception is thrown instead.

ToChar(c: char): char

No operation.

ToChar(i: int): char

Converts an integer to the equivalent Unicode character.

ToInt(c: char): int

Returns the Unicode character number of a char.

ToInt(i: int): int

No operation.

ToInt(r: real): int

Truncates the real value to an integer (rounding towards zero).

ToList(s: set): list
ToList(l: list): list
ToList(s: string): list
ToList(o: object): list
ToList(p: pieceset): list

Enumerates all the elements of the argument and returns a list. (See Every Statement)

ToInt(s: string): int

Converts a string to the numeric equivalent.

ToReal(c: char): real

Same as ToReal(ToInt(c)).

ToReal(i: int): real

Converts an integer to a real.

ToReal(r: real): real

No operation.

ToReal(s: string): real

Converts a string to a real value.

ToSet(s: set): set
ToSet(l: list): set
ToSet(s: string): set
ToSet(o: object): set
ToSet(p: pieceset): set

Enumerates all the elements of the argument and returns a set. (See Every Statement)

ToString(x): string

Converts a value to its string representation.

Trap(x):object

Executes x and returns the exception object that was caught. In case no exception is thrown in x, nil is returned. In addition, the exception object contains a field trace that has extra information why the exception occurred. This information is useful for logging unexpected exception events in your WebL programs.

Type(x): string

Returns the type of x (nil, int, real, bool, char, string, meth, fun, set, list, object, page, piece, pieceset, tag).


1. The class indicated must be a subclass of webl.lang.expr.AbstractFunExpr


Up Previous Next