3

I have read this article and this SO. And I understand the functionality of modifier. When modifiers are placed on a function with a spacing they will be evaluated in the order of placing thus creating an And condition. However, I would to understand how multiple modifiers can be placed so that combination of other Boolean conditions such as OR be evaluated?

For Instance,

modifier modA {
  // verify something
  _;
  // verify something
  _;
}
modifier modB {
  // verify something
  _;
  // verify something
  _;
}
function Fun() modA modB {
  // Do something
}

In this case, modA AND modB will be the execution. But is it possible to use them to form OR condition?

1 Answer 1

2

There is no direct way in solidity. I'll explain you why. Modifier is like a macro function while compiling it will replace your modifier with its actuval implementation. Here _; replace with your function implementation.

contract ModiferTesting {
   uint public a;
   uint public b;
   uint public c;

   modifier modA() {
    a = a + 1;
    _;
   }

   modifier modB() {
    b = b + 1;
    _;
    b = b + 1;
    _;
    b = b + 1;
    _;
  }

  function test() public modA modB {
    c = c + 1;
  }
}

If you debug, above code you can find a=1, b=3, c=3

Try to write an other modifier that check's or condition.

1
  • Thanks for replying! I understand the part that you explained. but isn't the original idea behind modifiers is to evaluate the preconditions? so if modA and modB only checks certain conditions, shouldn't be somehow possible to obtain other boolean results? Also how c=3? it should be 4, right? Commented Feb 14, 2018 at 18:17

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.