为什么带有明确指定的泛型类型的proc无法正常工作?

问题描述

如果显式指定了map的类型,示例将无法编译,为什么?

这有效

import sequtils

let nlist = @[1,2]
let slist: seq[string] = nlist.map(proc (v: auto): auto = $v)

但这不会:

let slist: seq[string] = nlist.map[int,string](proc (v: auto): auto = $v)

解决方法

请记住,map is just a normal proc接受两个参数,由于method call syntax,您碰巧可以将其用作 fetch("https://fantasy.premierleague.com/api/bootstrap-static/",{ method: "GET",headers: { "Content-Type": "application/json",},}) .then((res) => { return res.json(); }) .then((data) => { console.log("DATA: ",data); }) .catch((err) => { console.log("Error: " + err); }); ,但是有some limitations。实际上,如果您将该代码编写为普通的proc调用:

first.map(second)

然后,编译器不再被语法迷惑,并向您抛出正当理由:

错误:仅当嵌套proc用作另一个例程的操作数并且可以从预期的签名中推断出通用参数的类型时,它才能具有通用参数。