Converting an int type to a uint8

GeckStar

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?

zah

The random proc is defined to return an int. The fact that the max value is 256 in this example is not encoded in the type system (it could have been if random was taking static[int] as an argument).

"lossless conversion" means that a value of a smaller integer type can be converted to a value of a larger integer type (e.g. from int8 to int32). In the examples here, you are trying to perform a conversion in the other direction. You can get around the errors by making the conversions explicit:

let a = uint8(rand(256))
show a
# or
let b = rand(256)
show uint32(b)

p.p. you don't need to add the discardable pragma to procs which don't return values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Does converting UInt8(or similar types) to Int counter the purpose of UInt8?

Argument of type int is incompatible with parameter type Uint8*

Swift converting signed array of Int [int8] to unsigned array of Int [UInt8]

Binary operator '|' cannot be applied to operands of type 'Int' and 'UInt8'

Conversion of image type int16 to uint8

{(Int)} is not identical to UInt8

binary operator += cannot be applied to operands of type 'UnsafeMutablePointer<UInt8>?' and 'Int'

Why am I getting a compile error 'cannot use ... as type uint8 in argument to ...' when the parameter is an int

Why string cannot convert to other data type array except uint8 and int32?

How do I convert a numpy uint8 to the default int type in Python?

Converting []uint8 to float64

Converting [UInt8]? to image in IOs swift

Converting uint8 array to image

Arithmetic on uint8, int8

Dart: Converting an int to a Uint8List

Converting Type Int to Type Byte

How to cast int to Uint8 in Dart?

Convert Int to Array of UInt8 in swift

Convert Int to UnsafePointer<UInt8>

How to convert int[] to uint8[]

Why is the output of element-wise addition/subtraction different depending on whether my numpy array is of type int64 or uint8?

matlab comparing arrays of type uint8

basic haskell : Converting Type to Int

Why can I pass [UInt8] type to the UnsafePointer<UInt8> type parameter?

Converting string of "bytes" to numpy array of uint8

Check If Custom Class Not Nil --- Swift Is Converting It To A UInt8

Converting []uint8/[]byte to hash table GoLang

Swift - converting from UnsafePointer<UInt8> with length to String

Converting a double sine function uint8 in Simulink - unexpected results