Solidity: Replace _ with body in modifiers

Created on 6 Sep 2016  Â·  13Comments  Â·  Source: ethereum/solidity

To make the code a bit less cryptic:

modifier owneronly {
    if (address != msg.sender) {
        throw;
    }

    body;
}

(Follow up of #535)

breaking change feature language design

Most helpful comment

How about "yield"? It's a common used keyword and generally well understood.

On 06 Sep 2016, at 06:02, Dave Hoover [email protected] wrote:

So 'body' will be a keyword?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

All 13 comments

So 'body' will be a keyword?

How about "yield"? It's a common used keyword and generally well understood.

On 06 Sep 2016, at 06:02, Dave Hoover [email protected] wrote:

So 'body' will be a keyword?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

I don't think yield matches this use-case. Furthermore, I think we should stay with using a symbol. Admittedly, _ is not really "visible" (depending on your font), but words do not stand out as well, I would say.

The proper way to do this would be:

modifier owneronly[f] {
     if (address != msg.sender) {
        throw;
    }
    f();
}

and this also allows us to do

modifier owneronly[body] {
     if (address != msg.sender) {
        throw;
    }
    body();
}

and modifier owneronly would be identical to modifier owneronly[_] at which point we could even achieve backwards-compatibility.

(in all cases, the fact whether () should or has to be specified is debatable)

@obscuren I like yield but it could mislead into thinking that coroutines are supported.

@chriseth your proposal is nice, but how many people would actually use it? The default behaviour should reduce the possibility of errors.

@axic we can report a warning if [] is not used.

I still say there's nothing wrong with the _.

@axic we can report a warning if [] is not used.

I think it could be an acceptable way to go this way. Though we could start issuing a warning for _ and switch to body in the next breaking.

How would it look with param arguments with modifiers in this proposed schema?

I would like to revive this discussion.

I don't think yield matches this use-case. Furthermore, I think we should stay with using a symbol. Admittedly, _ is not really "visible" (depending on your font), but words do not stand out as well, I would say.

The proper way to do this would be:

modifier owneronly[f] {
     if (address != msg.sender) {
        throw;
    }
    f();
}

Instead of making the modifier explicitly mention the function, how about the other way around? That is, a modifier states a condition, and the function decides how to use it, for example as a pre or post condition.

modifier owneronly {
  require(owner == msg.sender);
}
modifier magic(uint x) {
    require(x == 0xcafe)
}
function f(uint x) pre[[owneronly, magic(x)]] public view {...}

This starts to get mixed up with invariants though...

Before we had require, modifiers were used in a very different way. We should collect the current use-cases for modifiers and then maybe restrict the syntax to "pre-conditions, potentially with side-effects" only.

Agree!

Was this page helpful?
0 / 5 - 0 ratings