gpt4 book ai didi

Coffeescript 1.7.1 意外的换行怪异

转载 作者:行者123 更新时间:2023-12-04 05:10:41 25 4
gpt4 key购买 nike

我有以下代码(大约一年前),用于在应用程序中处理邮政编码。今天我不得不改变一个值,现在突然我不能再编译它了。我在“hilleroed”中的“h”处意外换行。

class Application
postal_codes:
regions:
sjaelland:[0..4999]
fyn: [5000..5999]
soenderjylland: [6000..6200]
.concat [6261..6280]
.concat [6600..6899]
.concat [6041..6064]
.concat [6960]
.concat [7000..7079]
.concat [7200]
.concat [7250]
nordmidtjylland: [6900..6999]
.concat [7080..7199]
.concat [7182..7184]
.concat [7190..7190]
.concat [7260..9999]

office_postal:
ringsted: 4100
hilleroed: 3400
kgslyngby: 2800
odense: 5220
haderslev: 6100
esbjerg: 6715
herning: 7400
aalborg: 9200
aarhus: 8210
horsens: 8700

offices:
ringsted: [0..2690]
.concat [2770,2791]
.concat [4000..4030]
.concat [4050..4990]

hilleroed: [2700..2765]
.concat [2980..3670]
.concat [4040]

kgslyngby: [2800..2970]

odense: [5000..5985]

haderslev: [6000..6240]
.concat [6300..6622]
.concat [6630..6640]
.concat [7000..7080]
.concat [7182..7184]

esbjerg: [6261..6280]
.concat [6623]
.concat [6650..6879] # 6880 is herning.. meh
.concat [6881..6893]
.concat [6960]
.concat [7200..7260]

herning: [6880]
.concat [6900..6950]
.concat [6971..6990]
.concat [7270..7280]
.concat [7330..7680]
.concat [7800..7884]
.concat [8800]
.concat [8831]

aalborg: [7700..7790]
.concat [7900..7990]
.concat [8832]
.concat [8970]
.concat [9000..9999]

aarhus: [8000..8270]
.concat [8305..8340]
.concat [8355..8643]
.concat [8660..8670]
.concat [8830]
.concat [8840..8963]
.concat [8981..8990]

horsens: [7100..7173]
.concat [7190]
.concat [7300..7323]
.concat [8300,8350]
.concat [8653..8659]
.concat [8680..8783]

我终其一生都无法弄清楚出了什么问题。这是一个错误还是我错过了什么?如果我将代码粘贴到 coffeescript.org 上的在线编译器中,我会得到同样的错误。怎么修?

编辑:

我正在使用 CoffeeScript 1.7.1。它适用于 1.6.3。

更新:

这是一个意外错误,请参阅 https://github.com/jashkenas/coffee-script/issues/3408

最佳答案

问题出在 office 对象中。此代码失败:

offices:                           
ringsted: [0..2690]
.concat [2770, 2791]
.concat [4000..4030]
.concat [4050..4990]
hilleroed: [2700..2765]
.concat [2980..3670]
.concat [4040]
kgslyngby: [2800..2970]

虽然这通过了:
offices:                                                                           
ringsted: [0..2690].concat [2770, 2791].concat [4000..4030].concat [4050..4990]
hilleroed: [2700..2765].concat [2980..3670].concat [4040]
kgslyngby: [2800..2970]

如果您真的想保留换行符,请添加一些括号:
offices:                          
ringsted: ([0..2690]
.concat [2770..2791]
.concat [4000..4030]
.concat [4050..4990])
hilleroed: ([2700..2765]
.concat [2980..3670]
.concat [4040])
kgslyngby: [2800..2970]

编译器似乎对对象定义的结束位置感到困惑。如果你省略括号,你会得到这个js:
// without parens

var _i, _j, _k, _l, _results, _results1, _results2, _results3;

({
offices: {
ringsted: (function() {
_results3 = [];
for (_l = 0; _l <= 233; _l++){ _results3.push(_l); }
return _results3;
}).apply(this)
}.concat((function() {
_results2 = [];
for (_k = 2770; _k <= 2791; _k++){ _results2.push(_k); }
return _results2;
}).apply(this)).concat((function() {
_results1 = [];
for (_j = 4000; _j <= 4030; _j++){ _results1.push(_j); }
return _results1;
}).apply(this)).concat((function() {
_results = [];
for (_i = 4050; _i <= 4990; _i++){ _results.push(_i); }
return _results;
}).apply(this))
});

其中,当在节点 repl 中运行时,返回:
TypeError: Object #<Object> has no method 'concat'
这是有道理的,基于 js,但没有意义基于咖啡。这可能值得 submitting as an issue .

TLDR :当您希望将函数返回值分配给对象的字段时,请使用括号。

关于Coffeescript 1.7.1 意外的换行怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22356642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com