简体   繁体   English

在汇编脚本中按索引调用列表元素 map function 导致编译错误

[英]Calling list elements by index in assembly script map function causing compilation error

I'm using nodeJS along with assemblyscript to test out webassembly.我正在使用 nodeJS 和 assemblyscript 来测试 webassembly。 I tried writing a simple python style zip function hoping it would work as intended.我试着写了一个简单的 python 风格 zip function 希望它能按预期工作。 But for some reason every time I hit compile, it throws an error with basically no description of why it breaks.但出于某种原因,每次我点击编译时,它都会抛出一个错误,基本上没有说明它为什么会中断。

export function zip(list1:Array<string>, list2:Array<string>):string[][]{
  return list1.map<string[]>((i:string,d:i32):string[] => [i, list2[d]])
}

Attaching image of the error message on compilation on running: yarn asbuild附上运行时编译错误信息的图片: yarn asbuild

编译后的终端输出

this querstion was resolved here.这个问题在这里得到了解决。https://github.com/AssemblyScript/assemblyscript/issues/2571https://github.com/AssemblyScript/assemblyscript/issues/2571

Explanation of why this doesnt work.解释为什么这不起作用。 As the error suggests the code doesnt allow access to certain variables within closure (in this case; the map function).由于错误表明代码不允许访问闭包内的某些变量(在本例中为 map 函数)。 hence the solution is to copy the value onto a temporary variable outside the function and then calls the value from there.因此解决方案是将值复制到 function 之外的临时变量,然后从那里调用该值。

like so:像这样:

let tempList: string[] = []

export function zip(list1: string[], list2: string[]): string[][] {
    tempList = list2
    return list1.map((i: string, d: i32): string[] => [i, tempList[d]])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM