Summary
(this is a very small suggestion)
I believe providing a wrapper function around revert
that logs a value without evaluating a condition, in the standard library, would allow for easier access to better ergonomics and flexibility while implementing functions. Thus encouraging better code styling (at least in my opinion, which may not be shared by the team) .
As of now the only function (require
) that allows for both logging a value and reverting execution also evaluates a condition. There are cases where implementations could (arguably) be more concise and cleaner with a log_and_revert
function.
Basic Example
Imagine I want to:
- Retrieve sender’s indentity of current call.
- In case identity could be determined, do some operation that requires it.
- Otherwise, log a custom error.
Using the tools the library currently exposes, a developer would probably go down a path similar to this:
enum AuthorizationError {
InvalidSender: (),
}
fn foo() {
let sender_result = msg_sender();
require(sender_result.is_ok(), AuthorizationError::InvalidSender);
bar(sender_result.unwrap());
}
With access to a log_and_revert
function, the developer could probably be more encouraged to instead do:
enum AuthorizationError {
InvalidSender: (),
}
fn foo() {
match msg_sender() {
Result::Ok(identity) => bar(identity),
_ => log_and_revert(AuthorizationError::InvalidSender),
}
}
Conclusion
I could be wrong, but for the code I’ve been seeing from the team it seems to me that the styling of the second implementation is preferred. Sure, the developer could simply call the log
function and then the revert
. But I think that including this functionality in the standard library aligns with Fuel/Sway’s philosophy.
This is a very small suggestion, that requires a very low level of effort to implement, but decided to share it either way.
Also, I’m not sure if this is the appropriate category to post this, or if I should have posted it over in Sway
. So sorry in advance if Sway
was the correct place.