0
import options

template p[T] = none(T)

discard p[int]

templat.nim(5, 10) Error: expression 'none(int)' is of type 'Option[system.int]' and has to be discarded

I think writing discard in front of the template instantiation is a reasonable enough way to do what the compiler asks, no ? Now it's just being grumpy.

EDIT: I've tried new things and it may be yet-another-case of very unhelpful compiler messages.

import options
template p[T](): untyped = T.none
discard p[int]()

This builds. The main change might be the untyped return type (note that typed didn't work either, with the same weird message).
And last flabbergast, T.none was fine but not none(T). I thought from UFCS both should be equivalent.

v.oddou
  • 6,476
  • 3
  • 32
  • 63
  • 1
    This is probably a good candidate for a bug report: https://github.com/nim-lang/Nim/issues – dom96 Apr 01 '18 at 11:26
  • @dom96 I suspect you refer to the last point, I took your advice, and posted a report here: https://github.com/nim-lang/Nim/issues/7461 – v.oddou Apr 01 '18 at 13:19

1 Answers1

3

By default, Nim will assume that the template returns a "statement list" (a block of Nim code, which doesn't denote any value). Since this must be a well-formed block, the return values of all calls inside it must be properly handled or discarded, hence you see the error.

To solve the problem, just add a return value to the template:

  import options

  template p[T]: auto = none(T) # notice that I added "auto" here!

  discard p[int]
zah
  • 5,314
  • 1
  • 34
  • 31
  • thanks ! more stuff are starting to make sense with lots of perseverance, but damn, Nim is not gentle, it's like a wild bison you have to tame. – v.oddou Apr 01 '18 at 13:20
  • There were multiple questions in my question, sorry about that, but now I still don't get why `untyped` (and `auto`) works, and `typed` does not. I thought historically, `untyped` was `StatementList` which means stuff like `if` which has no value. But here I return a well typed expression, so historically: `expr`, which means, `typed` today. The compiler asks me to discard the result though. – v.oddou Apr 01 '18 at 13:25