disnet / contracts.js

Contract library for JavaScript
http://www.contractsjs.org
MIT License
149 stars 8 forks source link

interop with unique structures like immutable.js #24

Open jasonkuhrt opened 9 years ago

jasonkuhrt commented 9 years ago

I am curious what thoughts you have about supporting something like immutable.js where the structural typing would make sense to the user but technically fail because contracts.js would not know how to work with immutable.js objects.

It would be useful if we could teach contracts.js new tricks.

disnet commented 9 years ago

So I haven't used immutable.js too much yet but this should just be a contract on the get/set interface right?

import @ from "contracts.js"

var Immutable = require('immutable');

@ let NumMap = {
    get: (Str) -> Num,
    set: (Str, Num) -> NumMap
}

@ () -> NumMap
function makeMap() {
    return Immutable.Map({
        a:"foo"
    });
}

var map = makeMap();

// contract violation because "foo" is a string
console.log(map.get("a"));

I think this at least gets the basics working. We might like some sweeter syntax though:

@ () -> Map Str => Num
function makeMap() {
    return Immutable.Map({
        "a": 1,
        "b": 2
    });
}

So perhaps we need a nice way to register new syntax?

jasonkuhrt commented 9 years ago

Some sort of extensible system would be great. I mean, at some point this gets type-y but maybe there is a light-weight way to create definitions. Unfortunately doubt I'll have time to contribute thoughts to this any time soon but will keep my eye on this project. Feel free to close!