Git Product home page Git Product logo

typified's Introduction

Typified

An experimental implementation of first class functional types using pure ES at runtime, inspired by Haskell, PureScript and Idris.

Travis Β  npm Β  License MIT Β  Coveralls Β  code style: prettier

npm: npm install typified --save
cdn: https://cdn.jsdelivr.net/npm/typified@latest/src/index.js

Getting Started

With Typified you introduce JavaScript to the world of strong typing in a functional fashion. Instead of the Java-style typing that TypeScript and Flow provide, Typified takes the Haskell-inspired approach where types are kept entirely separate from their associated values.

import κ“½ from 'typified';

const sayHello =
    t`sayHello ∷ String β†’ String` (name => `Hello ${name}!`);

By invoking sayHello with a String we're guaranteed to be returned a String otherwise Typified will throw a TypeMismatchError that will notify the developer of such an occurrence. Passing in anything other than String will throw the mismatch error.

Typified tries to stay close to the JavaScript world, and thus allows specifying the union operator (|) to accept multiple types. For instance our sayHello function could take a Number type as well and be perfectly fine, as it's not performing any String-specific operations – thus we could augment the types to allow Number to be passed too.

const sayHello =
    t`sayHello ∷ String|Number β†’ String` (name => `Hello ${name}!`);

Invoking sayHello with a Number type would be just as fine as invoking it with a String.

Typified also supports to the concept of generics. Below we have modified the function a little to accept two name parameters, which yields either one or two greetings depending on whether the names are the same.

const sayHello =
    t`sayHello ∷ String β†’ String β†’ String` ((first, second) => {
        return first === second
            ? `Hello ${first}!`
            : `Hello ${first} & ${second}!`;
    });

We've removed the Number type constraint as we always want to compare String types with each other. Invoking sayHello as sayHello('Adam', 100) would be somewhat pointless as a String and Number can never match with strict equality. However we're still performing a non-String exclusive operation, and therefore being able to pass a Number would still be rather useful.

You might be tempted to define the type as String|Number β†’ String|Number β†’ String however that would still allow the passing of a String and a Number at the same time. What we need is a way to enforce any type as long as both match – generics suddenly become a useful tool.

const sayHello =
    t`sayHello ∷ βˆ€ a. a β†’ a β†’ String` ((first, second) => {
        return first === second
            ? `Hello ${first}!`
            : `Hello ${first} & ${second}!`;
    });

Using generics we have the above type annotation which takes a generic a – the a doesn't yet have a concrete type, and will instead assume a concrete type when the function is invoked. Passing a Number as firstName would cause a to be of type Number, and therefore ensure that secondName is also a Number. Likewise passing String as the first parameter would give a a type of String.

You can define as many generics as you wish in the type annotation, but it's crucial that you define them using βˆ€ or forall as otherwise the types would be assumed to be concrete types. In Haskell it's perfectly valid to not define generics, whereas in PureScript the defining of generics is mandatory – Typified also takes this approach.

typified's People

Contributors

wildhoney avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

typified's Issues

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.