ink
ink copied to clipboard
Support Result<Self, E> returning ink! constructors
Currently it is not possible to return anything but Self in ink! constructors.
This is an artificial limitation created to make things a bit easier, however, technically as well as from a design perspective nothing speaks against introducing fallible ink! constructors.
They will behave in the same way to fallible ink! messages in that they will revert state in case Result::Err is returned.
Mainly ink_lang_ir as well as ink_lang_codegen crates are required to be modified in order to implement this feature.
After this change ink! constructors can either return Self or Result<Self, E> where E is some error type:
use ink_lang as ink;
#[ink::contract]
mod contract {
#[ink(storage)]
pub struct Contract {
value: Balance,
}
enum Error {
TooMuchInitValue,
}
impl Contract {
#[ink(constructor)]
fn new() -> Self {
self.value = 0;
}
#[ink(constructor)]
fn with_balance(balance: Balance) -> Result<Self, Error> {
if balance >= 1000 {
return Err(Error::TooMuchInitValue)
}
Self { value: balance }
}
}
}