Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

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:

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

Is there a conventional way to do it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are initializing an uint8[] with an int[] expression. 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[].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...