首页 > 解决方案 > Nim - 必须丢弃 int 的函数类型

问题描述

我是 Nim 的新手,为了好玩而写了这个简单的代码:

var x: int = 3
var y: int = 4
if true:
    y = 7

else:
    x = 7

proc hello(xx: int, yy: int, ): int =
    return xx + yy

hello(x, y)

代码看起来不错(我检查了 Nim 手册),但它给出了这个奇怪的错误:

c:\Users\Xilpex\Desktop\Nim_tests\testrig.nim(12, 6) Error: expression 'hello(x, y)' is of type 'int' and has to be discarded

为什么我会收到此错误?有什么我可以解决的吗?

标签: indentationnim-lang

解决方案


您收到一个错误,因为声明为返回值的 procs 意味着在某处使用该值,因此编译器会提醒您忘记了调用的结果。如果有时您想要结果,而有时您想要忽略它,您可以使用丢弃语句或将 proc 声明为{.discardable.}.


推荐阅读