Most modern compilers like Rust and Swift support special set arithmetic operations which cause to exceptions when result is overflow or underflow.
-
In Swift all integer operations checked by default but support special operations like &+, &- and &* for unchecked operations.
-
Rust has much more clever approach. In Debug mode all operations overflow checked but in release all kind of operations are wrapped if not specify flags like -Z force-overflow-checks. In the same time Rust provide wide range builtin methods for handling overflow behaviour per operation like: a.wrapping_add(b) (allow overflow), a.saturating_add(b) (like clamp(res, i32::MIN, i32::MAX)), a.overflowing_add(b) (return (i32, bool) tuple) and a.checked_add(b) (return Option<i32>)
So I propose add similar feature for AssemblyScript in this way:
Unchecked (wrapped/overflowed) operations (default for now)
a + b
a - b
a * b
a / b
a % b
a++
--a
!a
Checked operations which produce unreachable exception in runtime during overflow:
a !+ b
a !- b
a !* b
a !/ b
a !% b
a!++
--a!
!a // <-- need figure out how handle this. Hmm, may be `!a!`?
Why !+, !* and etc?
First of all it has great semantic look ! which mean warning, exception and exclusion. Secondary symbol ! on TS after variables is legit and ignore during transpiling except case when a: i32 | null. When a define as optional type in my opinion we could write something like this a! !+ b which also legit.
Why this important?
This protect code from bugs. This especially important for smart contracts and history of Ethereum/EVM knows many examples when ignoring overflow costs a lot of money.
WDYT?
Most modern compilers like Rust and Swift support special set arithmetic operations which cause to exceptions when result is overflow or underflow.
In
Swiftall integer operations checked by default but support special operations like&+,&-and&*for unchecked operations.Rusthas much more clever approach. In Debug mode all operations overflow checked but in release all kind of operations are wrapped if not specify flags like-Z force-overflow-checks. In the same timeRustprovide wide range builtin methods for handling overflow behaviour per operation like:a.wrapping_add(b)(allow overflow),a.saturating_add(b)(likeclamp(res, i32::MIN, i32::MAX)),a.overflowing_add(b)(return(i32, bool)tuple) anda.checked_add(b)(returnOption<i32>)So I propose add similar feature for AssemblyScript in this way:
Unchecked (wrapped/overflowed) operations (default for now)
Checked operations which produce
unreachableexception in runtime during overflow:Why
!+,!*and etc?First of all it has great semantic look
!which meanwarning,exceptionand exclusion. Secondary symbol!on TS after variables is legit and ignore during transpiling except case whena: i32 | null. Whenadefine as optional type in my opinion we could write something like thisa! !+ bwhich also legit.Why this important?
This protect code from bugs. This especially important for smart contracts and history of Ethereum/EVM knows many examples when ignoring overflow costs a lot of money.
WDYT?