E4008

E4008#

FFI 函数不能有类型参数。

错误示例:#

pub extern "js" fn id[T](x : T) -> T = "(x) => x"

这个例子中,FFI 函数(用 extern 标识)带有一个类型参数,而这是不允许的。

建议#

考虑使用符合你需求的具体类型:

///|
extern "js" fn int_id(x : Int) -> Int = "(x) => x"

///|
pub fn concrete_id(x : Int) -> Int {
  int_id(x)
}

对于更复杂的场景,考虑添加一个额外的特征:

///|
pub(open) trait Ider {
  id(Self) -> Self
}

///|
impl Ider for Int with id(self) {
  concrete_id(self)
}

///|
pub fn trait_id(x : Int) -> Int {
  Ider::id(x)
}