Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nim - Function type of int has to be discarded

I am new to Nim, and wrote this simple code for fun:

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)

The code seems fine (I checked with the Nim manuals), but it gives this weird error:

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

Why am I getting this error? Is there something I can do to fix it?

like image 952
xilpex Avatar asked Sep 18 '25 12:09

xilpex


1 Answers

You are getting an error because procs declared to return values are meant to use that value somewhere, so the compiler reminds you that you are forgetting the result of the call. If some times you want the result, and others you want to ignore it, instead of creating a temporal variable you can use the discard statement or declare the proc as {.discardable.}.

like image 174
Grzegorz Adam Hankiewicz Avatar answered Sep 23 '25 11:09

Grzegorz Adam Hankiewicz