1

How does object composition work in Solidity? I haven't found a comprehensive guide, and all examples seem to deal with hello world level stuff or ERC20 token implementations.

  1. Can I have a contract with public properties which are other contracts?
  2. Can these contract instances passed as function arguments between contracts?
3

Yes and Yes

contract Foo {
    function Foo() {}

    function chop() public returns (bytes32)
    {
        return "Chop!!!";
    }
}

contract Bar {
    // Foo bytecode will be compiled into Bar's bytecode
    Foo public foo = new Foo(); 

    // Foo bytecode will be loaded into memory from an existing Foo deployment
    Foo public foo2 = Foo(0xF00BAA...); 

    // An address primitive to cast;
    address public hoo;

    // Internal calls can pass complex types
    function kungFoo(Foo sumFoo) internal {
        sumFoo.chop();
    }

    // External calls ca pass basic types
    function kungHoo(address sumHoo) public {
        hoo = sumHoo;
        Foo(hoo).chop();
    }
}
1
  • I haven't tested this yet, but assuming a Foo (complex instance type) can't be passed in externally? There wouldn't be much need for it in any case – GViz Nov 27 '18 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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