简体   繁体   English

无法使用 uint8 类型初始化数组

[英]Can't init array with uint8 type

I try to init array with the following expression:我尝试使用以下表达式初始化数组:

const a: array[3, uint8] = [1, 2, 3]
echo repr(a)

Compiler output:编译器 output:

Error: type mismatch: got <array[0..2, int]> but expected 'array[0..2, uint8]'

Is there a conventional way to do it?有传统的方法吗?

You are initializing an uint8[] with an int[] expression.您正在使用 int[] 表达式初始化 uint8[]。 In order to initialize it properly, you need to use compatible literals.为了正确初始化它,您需要使用兼容的文字。

For example:例如:

const a: array[3, uint8] = [1'u8, 2, 3]

By marking the initial element of an array expression as an uint8 using the 'u8 suffix, you make the whole array expression an uint8[].通过使用 'u8 后缀将数组表达式的初始元素标记为 uint8,可以使整个数组表达式成为 uint8[]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM