Converting an int type to a uint8

Viewed 2172

The Nim Tutorial page states:

Lossless Automatic type conversion is performed in expressions where different kinds of integer types are used.

So, I thought that creating an int in the range of a uint8 would allow me to pass it to procs expecingt an uint8.

However, the following code raises the annotated errors:

import random

proc show(x: uint8) {.discardable.} = echo x

let a: int = 255
show(a)  # type mismatch: got (int) but expected one of proc show(x: uint8)

let b: uint8 = random(256)  # type mismatch: got (int) but expected 'uint8'
show(b)

I am very confused by the first one, which is telling me it expected a procinstead of an int. The second one is clearer, but I expected an autoconversion at this point, since random generates an int in the uint8 range (0..256) (documentation).

Is there a way to convert int to uint8?

1 Answers
Related