I am new to nim, having written my first program several days ago, and so far I like the language. However, I think I have found a bug in nim-2.2.4's checking of values for range types. The same behavior occurs in your online trial version 2.2.0. I have code and sample output for this, but when I tried to post it, my post was rejected for not conforming to rst format rules. I'm not familiar with rst, so I'm just going to describe the problem in this post, and if someone could point me to an example of a forum post in its original rst form I will be happy to include the code and output.
To demonstrate the bug (if that's what it is), first create a range type of the form "0..10000". Then declare a variable of that type and use the type name as a constructor function with an integer argument to set the variable's value. If the integer is in range, that works fine. Then try calling the constructor with an out of range value like 13827. As expected, that gets an error message like "Error: unhandled exception: value out of range: 13827 notin 0 .. 10000 [RangeDefect]". But now try it with a float argument like 13827.0. Unexpectedly, that succeeds and you end up with a variable with the range type but an out of range value.
Is this an actual bug or am I just confused? As I said, code and output are forthcoming if I can figure out how to post it.
Thanks.
Nim forum supports fenced code blocks:
```nim
echo "Code goes here."
```
You could also share a link to Nim playground (press "share with pasty.ee" at the bottom of the webpage) if you couldn't post it otherwise.
Also, I could reproduce your bug:
type A = range[0..10]
let f = 156.0
let a = f.A
echo a # 156
It is already reported on github: https://github.com/nim-lang/Nim/issues/7179
Here is the code for my range error bug test, and its output when run on the online nim trial. Note that it first shows the failure of nim to detect an out of range float arg to the range type call, followed by its successful detection of an out of range integer arg.
import osproc
let output = execCmd( "uname -a; echo; nim --version")
echo output
echo ""
type Scor = range[ 0..10000] # position evaluation scores.
let x = 13827.0
echo $x
var scor = Scor( x)
echo $scor
echo $scor.type
echo ""
let i = 13827
scor = Scor( i)
echo $scor
echo $scor.type
Linux 534ad7cdc263 5.15.0-118-generic #128-Ubuntu SMP Fri Jul 5 09:28:59 UTC 2024 x86_64 Linux
Nim Compiler Version 2.2.0 [Linux: amd64] Compiled at 2024-10-02 Copyright (c) 2006-2024 by Andreas Rumpf
git hash: 78983f1876726a49c69d65629ab433ea1310ece1 active boot switches: -d:release 0
13827.0 13827 Scor
Error: unhandled exception: value out of range: 13827 notin 0 .. 10000 [RangeDefect]