1

In external vs public best practices, Tjaden Hess says that:

It almost never makes sense to use the this.f() pattern, as this requires a real CALL to be executed, which is expensive

So why does solidity even allow it in the first place? Wouldn't it make more sense to get warning to mark the function as public and call it as usual with f()?

1

I find myself challenged to think of a reason, because it's almost never necessary and is more expensive. It does, however, change the context, so it is useful to know that this tool is in the box.

Consider the flows and the context.

Alice => this.something() => f()

in f(), msg.sender is Alice and msg.value is whatever Alice sent.

Or

Alice => this.something() => this.f()

in f(), msg.sender is this and msg.value is whatever something() sent, which is probably 0.

The former is because f() would be running in the context of Alice's message. The latter is because a new message was initiated from this back to this. Consequently, it has its own context.

Hope it helps.

UPDATE

Thanks, Steve Marx, for some ideas about why this might be useful in some cases. You can carve off a portion of the ether or the gas and you can also carry on if f() fails, of you want to (not generally recommended). You would have to use call() to invoke this.f() to access such possibilities as .gas(), like this: What does Solidity's "call" function mean?. It will return (bool, bytes) where the bool is the success/failure of f() and the outer function (this) has a choice to continue or revert.

  • You are "challenged to think of a reason" and you still provide a long answer 9 minutes after the question was asked :) – Lauri Peltonen Sep 22 at 18:14
  • 1
    It's also an opportunity to forward a portion of the available gas, make available a portion of the ether sent with the transaction, and "try/catch" the call in case it reverts (though this last bit is only possible if you use a low-level call... Solidity's this.f() will automatically bubble up the revert). – user19510 Sep 22 at 18:16
  • Those are good possible reasons :-) although I hope it doesn't encourage try/catch in smart contracts outside of extremely novel cases. – Rob Hitchens - B9lab Sep 22 at 20:02

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.