Dependencies

There are three basics ways to add external dependencies:

  • extern: from the global scope (extern console)
  • import: from other dependencies (import 'fs' for readFile)
  • require: from the module parameters (require foo -> module.exports = function(foo))

Three combined ways:

  • extern|require: first look into the global scope, and if not found, look into the module parameters
  • require|extern: first look into the module parameters, and if not found, look into the global scope
  • require|import: first look into the module parameters, and if not found, import it

❗ All dependencies can only be used at the root of the module. No import inside a function.

extern

extern statements define external elements. Usually, those elements are from a global context defined by the engine on which the code will be executed.

examples

extern sealed namespace Math
extern sealed class Number {
toString(): String
}

require

If a file doesn't have the attribute #![bin], then it is a module.

The following module:

extern console
console.log('Hello world!')

for ES6, generates:

module.exports = function() {
console.log('Hello world!');
}

require define the parameters required by the module.

examples

require Color: class
require {
enum Space<String> {
RGB
}
class Color {
space(): Space
space(space: Space): Color
}
}

extern|require

extern|require will look for the item in the global context, if not found, look into the module parameters.

example

extern|require sealed class Array

require|extern

require|extern will look for the item in the module parameters, if not found, look intothe global context.

example

require|extern sealed class Array

require|import

require|import will look for the item the module parameters, if not found, it will import them.

example

require|import 'array' for Array

disclose

disclose is used to reveal members of external classes or namespaces to kaoscript.

syntax

disclose varname {
(field | method)*
}

field = varname[: type]

method = [async] varname([[parameter1] [, [parameter2] ... [, [parameterN]]])[: type] [~ class1 [, class2 ... [, classN]]]

examples

extern sealed class String
disclose String {
length: Number
substring(indexStart: Number, indexEnd: Number = -1): String
toLowerCase(): String
toUpperCase(): String
trim(): String
}
extern sealed namespace Math
disclose Math {
PI: Number
abs(value: Number): Number
}

sealed

sealed is a modifier for dependencies to indicate that the dependency isn't directly modifiable.
It mostly used to declared native classes like Array or String.

implements & native class

impl doesn't modify classes that have been flagged as sealed.

extern sealed class Number
// import the external class 'Number' and flag it as `sealed` to avoid to directly modify it
impl Number {
mod(max: Number): Number {
if this == NaN {
return 0
}
else {
const n = this % max
if n < 0 {
return n + max
}
else {
return n
}
}
}
}
// adds the method 'mod' to the class 'Number'
// under the hood, the object `__ks_Number` is created to avoid to modify the object 'Number'
let i = 42
// inferred type: Number
console.log(i.mod(2))
// 0
// javascript code: console.log(__ks_Number._im_mod(i, 2))