mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Tests for suffixes used to define operators
This commit is contained in:
parent
2c7ab0cf48
commit
79342f45e7
@ -0,0 +1,24 @@
|
|||||||
|
type B is bool;
|
||||||
|
using {bitnot as ~, bitor as |} for B global;
|
||||||
|
|
||||||
|
// NOTE: There are no literals of type B so these cannot be actually used on anything
|
||||||
|
function bitnot(B x) pure suffix returns (B) {
|
||||||
|
return B.wrap(!B.unwrap(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
function bitor(B x, B y) pure suffix returns (B) {
|
||||||
|
return B.wrap(B.unwrap(x) || B.unwrap(y));
|
||||||
|
}
|
||||||
|
|
||||||
|
contract C {
|
||||||
|
function testBinary() pure public returns (B) {
|
||||||
|
B.wrap(true) | B.wrap(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testUnary() pure public returns (B) {
|
||||||
|
-B.wrap(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// testUnary() -> true
|
||||||
|
// testBinary() -> false
|
@ -0,0 +1,10 @@
|
|||||||
|
type Int is int;
|
||||||
|
using {add as +, unsub as -} for Int global;
|
||||||
|
|
||||||
|
function add(Int x, Int y) pure suffix returns (Int) {
|
||||||
|
return Int.wrap(Int.unwrap(x) + Int.unwrap(y));
|
||||||
|
}
|
||||||
|
|
||||||
|
function unsub(Int x) pure suffix returns (Int) {
|
||||||
|
return Int.wrap(-Int.unwrap(x));
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
using {add as +, unsub as -} for uint global;
|
||||||
|
|
||||||
|
function add(uint x, uint y) pure suffix returns (uint) {
|
||||||
|
return x + y;
|
||||||
|
}
|
||||||
|
|
||||||
|
function unsub(uint x) pure suffix returns (uint) {
|
||||||
|
return uint(-int(x));
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// TypeError 8841: (0-45): Can only use "global" with user-defined types.
|
||||||
|
// TypeError 5332: (7-10): Operators can only be implemented for user-defined value types.
|
||||||
|
// TypeError 5332: (17-22): Operators can only be implemented for user-defined value types.
|
Loading…
Reference in New Issue
Block a user