2

Question in one sentence: Are there seperate implicit type conversion rules for function call resolution (other than the conversion rules for the remaining cases)?

Regarding the array type conversions in Solidity, it seems that the smaller size of types can be implicitly converted into the larger size of types (if the base element types are equal). For example, when we have uint[2] u2; uint[3] u3;, u2 can be assigned into u3 (u3:=u2;), but not vice versa.

My confusing part is that, the situation above does not apply in function call resolutions, i.e., in matching types between argument and formal parameters. Below is an example.

Is it a normal situation ?

contract Test{

    uint[2] public u2=[1,2];
    uint[3] public u3;


    function test() public{
        u3=u2;
        test2(u2); // compile error !!
    }

    function test2(uint[3] arr) internal{ }
}

1 Answer 1

0

there is not! type conversion is the same, where ever it happens, it follows the same rule(s), function calls, code blocks, or where ever inside the solidity scope.

your example above has a compiler error because its a run time error, and not a logical or syntax error. it will fail when the code runs.

4
  • Why does the code has a run time error? In my example, the statement u3=u2; can be successfully executed; I checked this in Remix with solc(v 0.7.4).
    – formal
    Commented Nov 24, 2020 at 6:15
  • its clear. the function test2 expects three ints, which is 256x3 bits, but it receives only two ints which is 256x2 bits
    – ali
    Commented Dec 1, 2020 at 0:35
  • Then why does u3=u2 work without runtime or compilation errors? At the function call site test2(u2), it can be thought that there is an assignment arr=u2 (where arr is a formal parameter of test2). In other words, I wonder why u3=u2 works but arr=u2 does not work. This is the confusing part to me and why I asked question.
    – formal
    Commented Dec 2, 2020 at 1:08
  • see this part of documentation which addresses your question link
    – ali
    Commented Dec 5, 2020 at 1:49

Your Answer

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

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