Code Monkey home page Code Monkey logo

overloads-in-javascript's Introduction

overloads.js

This library features adding support of runtime types, function overloading and context management within JavaScript.

Install

To install the package use npm:

npm install overloads.js

Files

For examples go to the src/examples.js file. The implementation is contained in src/polymorph.js file.

Documentation

Constants

1. PRIMITIVE_TYPES

Definition (from polymorph.js file):

const PRIMITIVE_TYPES = Object.freeze([
	"any",
	"object",
	"function",
	"string",
	"number",
	"boolean",
	"undefined",
	"symbol",
	"bigint",
])

Functions

1. polymorph

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.

2. polymorphClass

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.

3. primvarinit

Defines a variable, containing a value of primitive type.

4. classvarinit

Same as primvarinit, but for classes.

5. primitiveValueCheck

Checking function for primitive values.

6. classValueCheck

Same as primitiveValueCheck, but for classes.

7. varinit

Define a variable with an arbitrary value and checking function (if any).

8. setcurrcontext

Change the current variable and function context to another one.

Note:

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).

9. getcurrcontext

Returns current context.

10. varread

Return a value of a variable in a given context.

11. varset

Set a value for a variable in a given context.

12. defineFunc

Defines a function in a given context with the syntax similar to that of polymorph or polymorphClass.

13. getFuncRef

Returns info (including the reference) to a function whose name and context are given.

14. callFunc

Calls the function by a given name and context.

15. makeContext

Creates a new context by a given name or erases all information about the already existing one.

16. deleteContext

Deletes an already existing context.

17. printVar

Prints the info of a defined variable in a given context.

18. contexts

Returns all of info of the currently existing contexts.

Classes

1. Any

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).

2. SpecialValue

Allows to create any arbitrary keyword-marked type and use it with a certain value. Can be inherited and given a different value:

Properties:
1. name

The name of the created special type. A private property, can be obtained through the SpecialValue.getType()

2. value

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.

3. randomfunc

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().

Methods
Note:

Here, I won't list the getValue(), getName() and getGenerator() methods. They are just getters for fields above.

1. inherit(value)

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.

overloads-in-javascript's People

Contributors

hgargg-0710 avatar hgargg avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.