@cameel: OK, got it working!
Now, following your example, I have added this at the top of my contract:
pragma solidity ^0.8.17;
import "../NaturalNum.sol";
using {NaturalNum.add as +} for uint256[];
Where the actual contract is:
contract NaturalNumUser {
function add(
uint256[] memory x,
uint256[] memory y
) external pure returns (uint256[] memory) {
return NaturalNum.add(x, y);
}
}
I’d like to replace return NaturalNum.add(x, y)
with return x + y
of course.
But before even trying that, the compiler doesn’t seem to accept the new syntax:
DeclarationError: Identifier not found or not unique.
--> project/contracts/helpers/NaturalNumUser.sol:6:8:
|
6 | using {NaturalNum.add as +} for uint256[];
| ^^^^^^^^^^^^^^
Error HH600: Compilation failed
And if I change pragma solidity ^0.8.17
to pragma solidity 0.8.18
, then I get:
ParserError: Source file requires different compiler version
(current compiler is 0.8.18-ci.2023.1.17+commit.e7b959af.Darwin.appleclang) -
note that nightly builds are considered to be strictly less than the released version
--> project/contracts/NaturalNum.sol:2:1:
|
2 | pragma solidity 0.8.18;
| ^^^^^^^^^^^^^^^^^^^^^^^
Any suggestions?
UPDATE:
The reason for the Identifier not found or not unique
error, believe it or not, is the fact that I have an overloaded private function of the same name, i.e.:
function add(...) private pure ...
So this definitely sounds like something that you need to fix in the compiler.
Trying “using {NaturalNum.eq as ==} for uint256
” instead, I get a different compilation error:
Operators can only be implemented for user-defined value types
This is because the compiler insists that I first define the specific type, for example:
type xxx is uint256;
using {NaturalNum.eq as ==} for xxx;
It is not really clear to me why this requirement is needed in the first place (i.e., why not just allow using uint256
as part of the using
statement?).
But in any case, my type happens to be uint256[]
, which the compiler doesn’t even support (“The underlying type for a user defined value type has to be an elementary value type”).
So in short, the new feature is not yet “ripe” for me to make use of.
BTW, it would be nice if you could add support for non-elementary user defined value types at some point (see this issue, which I have opened on your GitHub some time ago).
Thanks 