Hi I was hoping it was ok for me to make a thread like this since I found no other thread when using search. I am reading and writing all the examples in the Nim in Action book which is extremely helpful. But there are some things not explained. I was hoping beginners could post questions here to the more advanced users.
The "assert" and "doAssert" functions(using the word function loosely here), are in several examples like:
let numbers = @[1,2,3,4,5,6]
let odd = filter(numbers, proc (x: int): bool = x mod 2 != 0)
assert odd == @[1,3,5]
and:
doAssert getUserCity("Damien", "Lundi") == "Tokyo" doAssert getUserCity(2) == "New York"
I read that it has something to do with the compiler, but I still don't understand it.
Thank you for any answers, would be very helpful and much appreciated.
There is nothing special with assert(). See
https://nim-lang.org/docs/lib.html
https://nim-lang.org/docs/assertions.html#doAssert.t%2Cuntyped%2Cstring
Assert just ensures that a condition is true, otherwise program terminates with an error message.
Plain assert() is ignored when program is compiled with -d:release option, doAssert() is always active.
With 0.20, plain asserts are kept
Oh -- I thought bound checks are kept with -d:release, but not asserts.
I hope the differences of -d:danger and -d:release are explained at a prominent place now -- some people like me have trouble to remember. Some think still that -d:danger does include -d:release, but you told us recently that we have to apply both for utmost performance.
mratsim -- I don't get it.
You told us recently that -d:danger is not enough for optimal performance, so use additional -d:release.
And I think I tested that indeed. But from
https://nim-lang.org/docs/nimc.html#additional-compilation-switches
we get the impression that -d:danger is enough, adding additional -d:release would make no change.
Because -d:danger is "Turns off all runtime checks and turns on the optimizer."
I got last info from
https://forum.nim-lang.org/t/5124#32234
You need to compile with both -d:release (removes stacktraces and uses -O3) and -d:danger.
And indeed, just tested again:
$ nim c -d:danger board.nim
$ nim c -d:danger -d:release board.nim
So the executable is smaller with both options!
Nim Compiler Version 0.20.99 [Linux: amd64] Compiled at 2019-09-20
That was my chess game.
This assert == stuff keep popping up constantly in the code examples. Can I just remove them?
var collection: set[int16] assert collection == {}
is it the same as: if collection == {} and if it is false it gives conpiler error?
I was also confused by both danger/release (and maybe I still am).
@torarinvik, you can use echo instead of assert if you want a visual inspection of the output. Otherwise like @dom96 said, we can ask the program to make the inspection for us.
Please let us know if you upload the chess game :D . How many lines of code did it take if I may ask?
The chess game is available since a few years of course, it was one of my early tests for Nim and GTK.
https://github.com/StefanSalewski/nim-chess4
It is only a basic chess engine and a plain GTK GUI, maybe 1400 lines of code total. I am not sure if the version at github compiles and works with Nim 1.0, I have a modified version on my local PC.
Yes, it didn't work.
Seems to be fine.
No github issue. Should be obvious that you have to install gintro first.
nimble install gintro
$ git clone https://github.com/stefansalewski/nim-chess4.git
$ cd nim-chess4
$ nim c board.nim
$ ./board
Runs fine for me. (There may be an hidden issue with my custom hash table, in danger mode and long thinking time, may raise an exception. I may fix that next year eventually, should be trivial.)
$ nim -v Nim Compiler Version 0.20.99 [Linux: amd64] Compiled at 2019-09-20
The display is plain UTF-8 chess font.
nim -v Nim Compiler Version 0.20.99
Is there a reason why you didn't update to v1?
OK:
$ nim -v Nim Compiler Version 1.0.99 [Linux: amd64] Compiled at 2019-10-03 Copyright (c) 2006-2019 by Andreas Rumpf
git hash: c51857f4348823f647110e8a1ede07d76d93b7da active boot switches: -d:release
The chess game compiles and runs fine for me still. But I can test only on 64 bit linux, maybe he had an windows or mac issue with gintro? Or maybe he tried to compile with newRuntime, that can not work as gintro does not work yet with newRuntime, maybe it never will.