sanjay_si (7) [Avatar] Offline
#1
It says that
when false: 
is checked by compiler for syntactically correct

The compiler will still parse the code and ensure that the code is still syntactically valid, but the code will not be included in the resulting program. This is because the compiler checks when statements at compile-time.

However the following lines of code does not throw any error even if it is not correct:

when false:
  "blah blah blah blah..."
  var i = 0
  i = "string value - must throw an error"


Am I missing something?
dom96 (75) [Avatar] Offline
#2
Nice catch! It seems this is a bug: https://github.com/nim-lang/Nim/issues/4301
dom96 (75) [Avatar] Offline
#3
Although, I just noticed. Your example, syntactically speaking, is perfectly valid.
sanjay_si (7) [Avatar] Offline
#4
dom96 wrote:Although, I just noticed. Your example, syntactically speaking, is perfectly valid.


Hi Dom, first of all I want to thank you for writing such an excellent book on Nim. It ignited my passion to learn Nim and also became sponsor on bountysource. The book is ideal for experience programmer and they would be enjoying since it does not cover toy examples.

Coming to the point, syntactically the code is not correct. The statement
var i = 0 
implies i is an int. And then in next line it is assigned to string for which the compiler throws an error. Also the first line
"blah blah..." 
throws compiler error.
dom96 (75) [Avatar] Offline
#5
I'm happy to hear that you're enjoying the book!

Syntactically that code is correct, there is no syntax error in it. The error is a "semantic error" because the types are not compatible and the compiler doesn't check the body of a when false statement for semantic errors.