Functions
Inkling is not a general-purpose programming language. Instead, Inkling executes in the context of data streams during brain training. As a result, the language only supports basic function behavior.
Supported
- typed input and output (return) parameters.
- typed variables.
- conditional (if/else) statements.
Not Supported
- statefulness across function calls.
- loop constructs.
- direct or indirect recursion.
- try/catch and exceptions.
- function overloading.
- default parameters.
- variable number of parameters.
Unsupported function usage is reported as an error by the Inkling compiler and disables brain training.
Function calls
Function calls pause execution in the current execution context and start a new context to run the function. The calling context must provide all relevant parameters. And the types of all provided values must match (or be castable to) the declared parameter types.
The return statement ends execution of the function and returns the specified
value to the calling context. For functions with explicit return types:
- the type of the return value must match the provided return type.
- if more than one return statement is used, all return types must match.
- if the return type has an explicit range, return values outside the range are
rounded to the closest valid value in the range. For example, if the return
type is
number<1..3>and the calculated return value is5, the function returns3.
If a return type is not specified, the compiler infers the type based on the the returned values.
Example function calls
# Function call with no arguments
MyFunc()
# Function call with multiple arguments
MyFunc(X, Y)
Function definition
Function definitions in the global namespace include the function keyword,
a name, an optional list of typed input parameters, and an optional output
parameter type. Inline functions may omit the function name.
Function names follow standard Inkling identifier rules.
Standard function definition
function RadiansToDegrees(Radians: number): number {
return Radians * 180 / Math.Pi
}
...
var XDegrees = RadiansToDegrees(Math.Pi)
Inline function definition
var XDegrees = function(Radians: number):number {
return Radians * 180 / Math.Pi
}
Note
All functions must return a value, but the return type definition may be omitted if the return type can be inferred by the compiler.
Variables
Variables can only be declared within a function. All variables have function-level scope, even when declared within a subscope like an if/else statement. As a result, variable names must be unique within a given function.
Variable type definitions are optional when the compiler can infer the type from an initial value assignment. Explicit type declarations are required when type cannot be inferred.
Unassigned variable declaration
The following code declares a number variable constrained to the range 1 to 3
(inclusive).
var OneToThree: number<1 .. 3>
Assigned variable declaration
The following code declares and assigns a variable without assigning an
explicit type. The compiler can infer the type (number) based on the initial
value.
var TwoOrThree = 2
The following code declares a variable and assigns an initial value. To enforce a preferred enumeration restriction, an explicit type assignment is also provided.
var TwoOrThree: number<2, 3> = 2
Important
All variables must be assigned a value before use. Unassigned variables result in compiler errors.
Conditional execution
Inkling supports if/else if/else statements. Braces are required for all conditional code blocks.
if IsZero(Input) {
# 0 is a special value
return 0
} else if IsOdd(Input) {
# Convert odd numbers to 1
return 1
} else if IsEven(Input) {
# Convert even numbers to 2
return 2
} else {
# How did you even get here?
return -1
}