disnet / contracts.js

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

contract declarations on functions not of the form "function name(){}" #23

Open joelmccracken opened 10 years ago

joelmccracken commented 10 years ago

I can't seem to get anything to work except for the named function form. EX:

import @ from "contracts.js"

@ ({name: Str}, [...{loc: Num}]) -> Str
var calcAverageLoc =  }function(person, locArr) {
    var sum = locArr.reduce(function (l1, l2) {
        return l1.loc + l2.loc;
    });
    return "Average lines of code for " +
           person.name + " was " +
           sum / locArr.length;
}

var typoPerson = {nam: "Bob"};
calcAverageLoc(typoPerson, [{loc: 1000}, {loc: 789}, {loc: 9001}]);
SyntaxError: [macro] Macro `@` could not be matched with `() - > Str var...`
9: @ ({name: Str}, [...{loc: Num}]) -> Str
   ^
disnet commented 10 years ago

Yeah we need some syntax to wrap an arbitrary expression in a contract. Here are some possibilities:

var f = @ wrap {
  function () { ... }
} in (Num) -> Num

var f = @ wrap {
  function () { ... }
} (Num) -> Num

var f = @ (Num) -> Num wrap {
  function () { ... }
} 

I'm not really excited about any of these. Any ideas?

joelmccracken commented 10 years ago

Of all of them, I certainly like the last the best. I was thinking something like:

@wrap (Num) -> Num Foo.prototype.inc = function(val) {...};

would read the next expression and wrap the first function expression it comes to. I don't know how practical that would be though.

On Fri, Oct 10, 2014 at 6:29 PM, Tim Disney notifications@github.com wrote:

Yeah we need some syntax to wrap an arbitrary expression in a contract. Here are some possibilities:

var f = @ wrap { function () { ... }} in (Num) -> Num var f = @ wrap { function () { ... }} (Num) -> Num var f = @ (Num) -> Num wrap { function () { ... }}

I'm not really excited about any of these. Any ideas?

— Reply to this email directly or view it on GitHub https://github.com/disnet/contracts.js/issues/23#issuecomment-58724258.