【问题标题】:haskell-js example breaks in the coffeescript REPL but works from a file or the node REPLhaskell-js 示例在 coffeescript REPL 中中断,但从文件或节点 REPL 工作
【发布时间】:2014-02-25 19:31:22
【问题描述】:

我一直在尝试使用 Javascript haskell-js 库,但我偶然发现了 coffeescript REPL 的一个奇怪行为。

使用节点,以下示例按预期工作:

$ node
require('haskell');
> [1,2,3].map('+1');
[ 2, 3, 4 ]

但是使用coffeescript,它失败了:

$ coffee -v
CoffeeScript version 1.6.1
$ coffee
require 'haskell'
[1,2,3].map('+1')
TypeError: +1 is not a function
at Array.map (native)
at repl:3:15
at REPLServer.replDefaults.eval (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:27:28)
at repl.js:239:12
at Interface.<anonymous> (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:55:9)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)

但是,当我从文件中运行它时,它又可以工作了:

$ cat test.coffee
require 'haskell'
console.log([1,2,3].map('+1'))

$ coffee test.coffee
[ 2, 3, 4 ]

编译为 test.js 会产生以下文件:

$ coffee -c test.coffee && cat test.js
// Generated by CoffeeScript 1.6.1
(function() {
  require('haskell');
  console.log([1, 2, 3].map('+1'));
}).call(this);

现在我很困惑。这不是我测试的吗? (console.log 包装器在 REPL 中也没有任何区别。)

你能帮我理解为什么它在coffeescript REPL中不起作用吗?

【问题讨论】:

    标签: javascript coffeescript read-eval-print-loop


    【解决方案1】:

    当我使用nesh shell 重复你的情况时,我得到了同样的错误

    1800:~/myjs$ nesh -c
    CoffeeScript 1.7.1 on Node v0.10.1
    ....
    coffee> [1,2,3].map('+1')
    TypeError: +1 is not a function
    

    但是当我直接使用coffee REPL 运行时,它工作正常

    1802:~/myjs$ coffee -v
    CoffeeScript version 1.7.1
    1802:~/myjs$ coffee
    coffee> require 'haskell'
    ...
    coffee> [1,2,3].map('+1')
    [ 2, 3, 4 ]
    

    在可行的情况下

    coffee> console.log [1,2,3].map.toString()
    function () {
            var args, fn;
    
            fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
            return _method.call.apply(_method, [this, fn.toFunction()].concat(__slice.call(args)));
          }
    

    没有的地方:

    coffee> [1,2,3].map.toString()
    'function map() { [native code] }'
    

    换句话说,.maphaskell 版本并没有取代原生版本。所以我们需要检查haskell 是如何加载的。 nesh 没有 coffee 覆盖也有这个负载问题。我敢打赌更新你的Coffeescript 会解决这个问题。

    编辑:

    haskell 通过修改global 自行安装。

    大约 6 个月前(在 1.6.3 和 1.7.0 之间)有一个关于咖啡的 pull request,https://github.com/jashkenas/coffee-script/pull/3150 解决了 REPL 中“全局”上下文的使用 "使 REPL 使用全局上下文与节点 REPL 保持一致。"

    【讨论】:

    • 可能是,我的咖啡脚本版本是 1.6.1(我编辑了我的帖子)。当我可以访问更新的咖啡脚本版本时,我会尝试一下。
    • package.json for haskell 指定 "coffee-script": "~1.6.2",。它是用咖啡写的。 github.com/arthur-xavier/haskell-js
    • 已确认。它适用于最近的咖啡脚本版本。
    猜你喜欢
    • 2011-06-13
    • 2012-09-30
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2016-02-11
    • 2011-09-29
    相关资源
    最近更新 更多