7

I need some clarification on internal and external library calling in relation to whether a struct is passed by reference or copied or something else.

In this following library example, L_i() is internal, is compiled into the derived contract's bytecode and called by jump. The struct parameter _s is passed by reference and so _s.i will be updated contract wide (so to speak).

library lib_internal {
    struct LStruct { uint i; }
    function L_i (LStruct storage _s) internal {
        _s.i++;
    }

In the following external instance, and this is where I need help, is _s still passed by reference but within the context of the contract or is it copied in the DELEGATECALL data to L_i()?

library lib_external {
    struct LStruct { uint i; }
    function L_i (LStruct storage _s) public {
        _s.i++;
    }
3
  • 1
    Why not write some test code and find out? I've written dozens of tests for questions like this. What's nice is afterwards you have an example test you can run to verify everything in your environment is okay.
    – Paul S
    Sep 14, 2016 at 17:07
  • 2
    Which I had done, though staring at debug traces and stacks isn't the most enlightening experience... What I am finding though is that for simple functions, it's more bytecode efficient to just internal the library function.
    – o0ragman0o
    Sep 14, 2016 at 21:22
  • post your test as the answer, get credit. BTW I use events, stack traces are horrible... see relevant posts on stackexchange.
    – Paul S
    Sep 15, 2016 at 3:47

2 Answers 2

8

Finally found the answer in the docs...right at the bottom.

The only situation where no copy will be performed is when storage reference variables are used.

3

According to the docs on Libraries, structs (and dynamic arrays?) would be passed by reference for internal library functions: "Of course, calls to internal functions use the internal calling convention, which means that all internal types can be passed and memory types will be passed by reference and not copied." https://solidity.readthedocs.io/en/develop/contracts.html#libraries

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.