Skip to content

Features

Prakhar Srivastav edited this page Feb 6, 2016 · 8 revisions

JSJS

A general purpose strongly-typed functional language for the web

Features

Feature|Exists in JS?|What we plan to add| What we'll skip ---|---|--- | --- | ---- | ---- Type Safety | ❌ | Strong types | Type inference Immutable values | ✅ ES6 only | No variables - everything is a value | NA Immutable Data Structures | ❌ | Lists and Maps | Arrays and Sets Polymorphic Types | ❌ | Polymorphism for in-built types | User-defined types Functions as first-class objects | ✅ | Syntactic sugar | NA Compiles down to JS | NA | Support for both Node and browser | NA Namespaces | ❌ in browser, ✅ in node | top level namespaces | Nested, circular dependancies

Inspired by

  • OCaml
  • Scala
  • TypeScript

Features of the Language

  • Strong Type Safety:
    JavaScript is a dynamically typed language, with no real notion of type safety. A variable in JavaScript can be assigned to data of any type and this assignment can be changed through the course of the program. JSJS is be a strongly typed language with static type checking, like OCaml. Types will not be inferred, so the types must be explicitly stated while declaring values and defining functions.
    A strong type system is described as one in which there is no possibility of an unchecked runtime type error and does not permit any implicit type conversion for operations at runtime.
    Since JSJS compiles down to JavaScript, which is a dynamically typed language, types in JSJS merely act as annotations for type safety and will be erased during generation of the JavaScript code.

  • Immutable Values
    One of the hallmarks of functional programming is immutability of all values used in a program. Javascript is an imperative language, where state can be changed and variables can be reassigned with new values. JSJS will have no concept of variables and all assignments will be treated as values, and hence cannot be changed or reassigned during the running of the program. We have chosen the keyword val for assigning values to identifiers to express the concept of all values being immutable more emphatically.

  • Everything is an Expression
    Every statement in JSJS is an expression that evaluates to a result of some type, exactly like in OCaml. We will also provide a Unit type, to allow for side-effects such as printing and logging which don't evaluate to an explicit return type.

  • Immutable Data Structures
    An immutable or persistent data structure is one in which previous versions of the data structure are preserved when it is modified. Immutable data structures make programs more robust and easy to reason about. Data structures in JavaScript are completely mutable.
    JSJS will provide immutable implementations for Lists and Maps in the language, with a standard library for functions like List.hd and List.tl, and Map.get and Map.set.

  • Polymorphic Types
    Support for polymorphic types is another fundamental feature of functional programming. This allows functions to take arguments of generic types and perform some operation on them, without the function being required to explicitly know the underlying types of the argument. For eg., we can have a function that takes as an argument a list of any type, and performs some operation on it. This function can work with both lists of numbers and lists of strings. JSJS supports polymorphism for the built-in data types.

  • Functions as First-Class Objects
    Functions in JSJS are first-class objects. Functions can be passed as arguments to other functions, returned by other functions, and can be assigned to variables. JSJS also supports anonymous functions. JavaScript has support for functions as first-class objects, so JSJS provides some syntactic sugar to make their usage sweeter.

Clone this wiki locally