0

Let's say I want to have higher order function capabilities in my Solidity Dapp. Also I want to be able to pass in new functions as input to higher order functions contained within contracts that were deployed at an earlier date. (i.e. the argument function is part of a contract deployed later than the one taking it as input)

It seems like I could wrap a function in an interface and then treat the contracts that inherit that interface as functions. So then if I want to pass one function into another I just have to pass in the instantiated interface.

//Deploy the following at t = 0
interface condition{
    
    function conditionHolds(uint a) public returns(bool);

    uint foobar;

}

contract applyConditionsToData{

    condition[] laterDeployedConditions = new condition[100];

    uint counter = 0;

    function checkData(uint data, uint index){
        
        applyCondToData(data, laterDeployedConditions[index]);
        
    }

    function applyCondToData(uint data,  condition cond) returns(bool) {

        return(cond.conditionHolds(data));
        
    }

    function addCondition(condition xyz){
        
        laterDeployedConditions[counter] = xyz;
        counter++;
        
    }

}
//Then deploy the following at t = 10
contract muchLater is condition{
    
    function conditionHolds(uint a) public returns(bool){
        
        return(false);
        
    }
    
    function addToList(applyConditionsToData directory){
        
        directory.addCondition(this);
        
    }
}

If you look at the function "applyCondToData" it is a function that takes input type "uint" and "condition" and outputs a boolean. However the "condition" type is merely a wrapper for function "conditionHolds". So here I am passing in one function as input to another function and now I have achieved higher order function capabilities.

Am I missing something here? Does this make sense? Or is there something about the EVM that doesn't allow this?

When I look up functional programming on Ethereum it seems that isn't possible. However this approach seems like a simple workaround.

4
  • Since you are passing around a condition contract it looks more like OOP. – Ismael Oct 20 '20 at 13:28
  • My main question is with regards to contract upgradability. Some sources online seem to indicate that the storage layout cannot change, or that constructors are not possible in upgradable contracts. Is it fine to deploy the first part of the code, and then subsequently add many condition instances at a later time? – zunior Oct 20 '20 at 14:27
  • To be more specific lets say I wanted to write custom conditions at a later date. Could I simply add them 1 by 1 to the central registry? what restrictions does Ethereum have on this kind of upgradability? – zunior Oct 20 '20 at 14:48
  • Sorry I don't understand what are you trying to achieve. It appears to have similar purpose than EIP-2535: Diamond Standard – Ismael Oct 20 '20 at 20:06

Your Answer

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

Browse other questions tagged or ask your own question.