9

These two lines compile:

uint8[5] foo = [0,0,0,0,0];
int[5] foo = [1,0,0,0,0];

My question is, why do the following two lines not compile and how can I fix them?

int[5] foo = [1,-1,0,0,0];

Error: Unable to deduce common type for array elements

int8[5] foo = [0,0,0,0,0];

Error: Type uint8[5] memory is not implicitly convertible to expected type int8[5] storage ref.

I assume inlining numbers only works with uint?

Solidity Version 0.3.2-9e36bdda

1 Answer 1

7

The following code compiles correctly:

uint8[5] foo1 = [0, 0, 0, 0, 0];
int[5]   foo2 = [1, 0, 0, 0, 0];
int[5]   foo3 = [int(1), int(-1), int(0), int(0), int(0)];
int8[5]  foo4 = [int8(0), int8(0), int8(0), int8(0), int8(0)];

Q: why do the following two lines not compile and how can I fix them?

A: Because the compiler cannot deduce the correct types of the array elements to match the array type. See the code above to fix the issue. Reference Solidity Documentation - Types - Explicit Conversions.

Q: I assume inlining numbers only works with uint?

A: See Solidity Documentation - Types - Type Deduction.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.