TypedJS icon indicating copy to clipboard operation
TypedJS copied to clipboard

Does not work with functions not in global scope

Open goatslacker opened this issue 13 years ago • 8 comments

But you knew that already :)

goatslacker avatar Feb 09 '12 22:02 goatslacker

It actually does. See the new example on the readme. Though I agree that the syntax for making the declaration is not ideal:

MyObj = {
    //+ MyObj.test_fun :: Number -> Number -> Number
    test_fun:function(num1, num2){
        return num1 + num2;
    }
}

Ejhfast avatar Feb 09 '12 23:02 Ejhfast

Also, if you are looking through your fork, you may want to update things... I've already pushed some fairly significant changes/fixes.

Ejhfast avatar Feb 09 '12 23:02 Ejhfast

Oh no I saw that in examples/test.js I'm talking about the private functions inside a closure.

Example:

(function () {
  // no way to access concat automatically :(
  // but TypedJS throws an error!
  //+ concat :: String -> String -> String
  function concat(a, b) {
    return a + b;
  }
}());

I was thinking about this and perhaps there's two ways to attack this problem.

  • Use TypedJS along with your unit tests.
  • Allow one to call a single automated test from wherever.

goatslacker avatar Feb 09 '12 23:02 goatslacker

This is especially an issue for people who write JavaScript programs that don't leak any global variables and their public interface is limited.

goatslacker avatar Feb 09 '12 23:02 goatslacker

Oh, ok. That's a thornier problem. And the private scoping pattern is pretty common... I'll give it some thought.

Ejhfast avatar Feb 09 '12 23:02 Ejhfast

A crazy idea to have automated tests and private functions:

  • You parse the JS
  • Turn all the functions global in a typedjs sandbox
  • Assign them new names (in case of collisions)
  • Find/replace all references to those functions with their new names
  • Create the test suite with the new functions and signatures mapped to them
  • Run the automated tests
  • return results

goatslacker avatar Feb 09 '12 23:02 goatslacker

That's not so crazy, really. Proxino's error-detection product did something similar. We got a lot of flack for it from developers, but people might not care so much in this case (since TypedJS is for testing, not production code).

Ejhfast avatar Feb 09 '12 23:02 Ejhfast

I wrote it and it's working pretty ok. I haven't done the assigning of new names or find/replace all references yet but for now it works for functions which don't call other functions.

goatslacker avatar Feb 15 '12 05:02 goatslacker