Skip to content

Builtins

hhh edited this page Jul 15, 2022 · 33 revisions

Builtins

Built-in Dicts

These built-in dicts are namespaces with separate documentations:

Name Documentation Link
Class Class
String String
Array Array
Dict Dict
Function Function
Number Number
Math Math
JSON JSON
Promise Promise
Time Time

help

help(target)

Returns the help information of specific target.

injectHelp

injectHelp(helpInfo, target)

Inject help information into specific target. (Returns target after injection.)

Built-in Variables

Name Type
true boolean
false boolean
Infinity number
NaN number
null null

Placeholder

There's one more built-in variable: _, which is a magic variable in hxs. Whenever a sentence(an expression separated by semicolons) is evaluated, the result will be stored in _. (_ is initially null.) For example,

a = _;
x = 2;
b = _ + _;
c = String('c', _);
"----------------------+
| Now, a becomes null, |
|      b becomes 4,    |
|  and c becomes 'c4'. |
+----------------------";

The placeholder is useful in certain cases, where you just want to store something temporarily. However, if you need to store some value for future use, using ordinary variables should be a better choice.

Built-in Functions

if/for/while/raise/try

See Process Control.

exist/delete

See Basics#Variables.

getContextStore

getContextStore()

Returns current context store. (A dict that stores all the variables in current scope.)

import/export/getExports

See Module Usage.

typeOf

typeOf(value)

Returns a string('number'/'string'/'boolean'/'function'/'null'/'array'/'dict') according to the type of the given value.

same

same(a, b)

Returns true if a is the same as b; (That is, (a == b) || (isNaN(a) && isNaN(b)).) returns false otherwise.

keys

keys(dict)

Returns the keys of the given dict. (Invokes dict.__keys if available.)

remove

remove(arrayOrDict, indexOrKey)

A utility function that removes specific index or key. (Invokes arrayOrDict.__remove if available.)

sizeOf

sizeOf(arrayOrString)

A utility function that combines Array.sizeOf and String.sizeOf.

slice

slice(arrayOrString, begin = 0, end = sizeOf(arrayOrString))

A utility function that combines Array.slice and String.slice.

clone

clone(arrayOrDict, indexOrKey)

A utility function that combines Array.clone and Dict.clone.

indexOf

indexOf(arrayOrString, valueOrSubstring)

A utility function that combines Array.indexOf and String.indexOf.

lastIndexOf

lastIndexOf(arrayOrString, valueOrSubstring)

A utility function that combines Array.lastIndexOf and String.lastIndexOf.

includes

includes(arrayOrString, valueOrSubstring)

A utility function that combines Array.includes and String.includes.

print

print(data...)

Print all arguments to the console.

assert

assert(condition, errorMessage = 'assertion failed')

Perform an assertion:

if (!condition) {
    raise(errorMessage);
};

number

number(value)

Built-in function number converts the given value to a number according to the following rules:

  • If the value is already a number, return it as is;
  • If the value is a string, try to parse it as a number
  • If the value is a boolean, return 1 if it is true and return 0 otherwise; and return the result(NaN if it can't be parsed as a number);
  • Return NaN otherwise.

string

string(value, previewSize = 10, previewIndent = ' ')

Built-in function string converts the given value to a string according to the following rules:

  • If the value is already a string, wrap it with proper quotes and return it;
  • If the value is a number, return its default string representation;
  • If the value is a boolean, return 'true' if it is true and return 'false' otherwise;
  • If the value is an array, return it in preview format;
  • If the value is a dict, return it in preview format;
  • If the value is a function, return '<function>'.

Preview Format

When arrays/dicts are converted into strings, they are displayed as preview format. The option previewSize specifies how many elements/entries are displayed. (The rest will be omitted and ... wil be shown.) The option previewIndent sets the indentation string of preview display. When previewSize is a non-positive value, '<array>'/'<dict>' will be displayed instead; Otherwise, arrays are displayed as:

(size: X) [
    element0,
    element1,
    ...
]

And dicts are displayed as:

(size: X) {
    key0 -> value0,
    key1 -> value1,
    ...
}

boolean

boolean(value)

Built-in function boolean converts the given value to a boolean according to the following rules:

  • If the value is already a boolean, return it as is;
  • If the value is a number, return false if it is zero and return true otherwise;
  • If the value is a string, return false if it is empty and return true otherwise;
  • Return true otherwise(function/array/dict).

shortcuts

These are builtin shortcuts:

Shortcut Source
forEach Array.forEach
map Array.map
filter Array.filter
assign Dict.assign
isInvocable Function.isInvocable
invoke Function.invoke
bind Function.bind
isNaN Number.isNaN
getConstructorOf Class.getConstructorOf
isInstanceOf Class.isInstanceOf
Clone this wiki locally