Consider the following toy code for representing Probability types. Applying 
the `math.sum` function to an array/seq of Probabilities that sum to more than 
1.0 should return a RangeError, but it doesn't. However, exactly the same 
implementation as math.sum (see 
[https://github.com/nim-lang/Nim/blob/version-1-0/lib/pure/math.nim#L204)](https://github.com/nim-lang/Nim/blob/version-1-0/lib/pure/math.nim#L204\)),
 but no longer generic on T, does return a RangeError as expected.

> How is it that the generic function manages to create an invalid Probability 
> variable in the code below? Is this a compiler bug?
    
    
    import math
    type Probability = range[0.0..1.0]
    
    proc probSum(ps: openArray[Probability]): Probability =
      for p in items(ps):
        result += p
    
    let ps = @[Probability 0.5, 0.5, 0.5]
    
    # The following line should raise an error, but doesn't
    echo "Using math sum: ", sum(ps), " typeof: ", typeof(sum(ps))
    
    # This line raises and error
    # echo "Using probSum: ", probSum(ps), " typeof: ", typeof(probSum(ps))
    
    
    
    Run

Reply via email to