This library features adding support of runtime types, function overloading and context management within JavaScript.
To install the package use npm:
npm install overloads.js
For examples go to the src/examples.js file. The implementation is contained in src/polymorph.js file.
Definition (from polymorph.js file):
const PRIMITIVE_TYPES = Object.freeze([
"any",
"object",
"function",
"string",
"number",
"boolean",
"undefined",
"symbol",
"bigint",
])
This function essentially returns another function that does all the job, connected with types. It is supposed to be used with primitive types. The way typing and overloading works can be seen in the src/examples.js file.
This function is more flexible due to the fact that instead of primitives it uses classes for type-checking. Also, it allows for single untyped parameteres via the Any class. Again, to see how it works - welcome to the examples.
Defines a variable, containing a value of primitive type.
Same as primvarinit, but for classes.
Checking function for primitive values.
Same as primitiveValueCheck, but for classes.
Define a variable with an arbitrary value and checking function (if any).
Change the current variable and function context to another one.
A context is a set of functions and variables independent of other contexts. Their introduction allows for having many variables of same names in the program separated by contexts. (I.e. they could be interpreted like namespaces in C++ or objects in JS)
There is a global context (which is default), local (current one) and all others. setcurrcontext(contextName) essentially changes the value of 'local' to the given context (contextName is it's name).
Returns current context.
Return a value of a variable in a given context.
Set a value for a variable in a given context.
Defines a function in a given context with the syntax similar to that of polymorph or polymorphClass.
Returns info (including the reference) to a function whose name and context are given.
Calls the function by a given name and context.
Creates a new context by a given name or erases all information about the already existing one.
Deletes an already existing context.
Prints the info of a defined variable in a given context.
Returns all of info of the currently existing contexts.
Allows for storage of any value. Works like:
import { Any } from "overloads.js"
const a = new Any(42)
console.log(a.value)
Also, the resulting object is constant (i.e. doing a.value = 10
won't work).
Allows to create any arbitrary keyword-marked type and use it with a certain value. Can be inherited and given a different value:
The name of the created special type. A private property, can be obtained through the SpecialValue.getType()
Some value assigned to the instance of the type. Can be anything. By default takes the value from the SpecialValue.randomfunc property-function. A private property, can be obtained via the SpecialValue getValue() method.
A function to generate an arbitrary value for a new SpecialValue, inherited from the current one. A private property. Can be obtained via the SpecialValue.getGenerator(). By default equals Math.random().
Here, I won't list the getValue(), getName() and getGenerator() methods. They are just getters for fields above.
Returns a new SpecialValue object of the same type (name is the same), if value is not given, then the randomfunc() is used to find it.