4

Wondering if solidity supports operator overloading like python does?

Something like:

---WARNING PSEUDOCODE---

overload __+__ {
    if (type(arg1) == string && type(arg2) == string){
        return arg1.append(arg2);
    }
}

I would use this to rewrite the SafeMath library, as SafeMath adding/multiplying/etc. in a long string becomes very unreadable...

Cheers!

2
  • 2
    Solidity doesn't support operator overloading.
    – Ismael
    Commented Feb 1, 2018 at 21:02
  • @Ismael thank you! I do remember reading something about this in a Vitalik blog post, but I don't recall the specifics. Perhaps in the future...
    – Alex
    Commented Feb 2, 2018 at 5:47

3 Answers 3

3

The official solidity documentation only mentions function overloading.

There is no operator overloading in Solidity.

3

As of 0.8.19, you can now define custom operators for types https://docs.soliditylang.org/en/v0.8.19/contracts.html#using-for

https://github.com/PaulRBerg/prb-math/blob/main/src/ud60x18/ValueType.sol#L82

Is a good example where it states:

using {
    Helpers.add as +,
    Helpers.and2 as &,
    Math.div as /,
    Helpers.eq as ==,
    Helpers.gt as >,
    Helpers.gte as >=,
    Helpers.lt as <,
    Helpers.lte as <=,
    Helpers.or as |,
    Helpers.mod as %,
    Math.mul as *,
    Helpers.neq as !=,
    Helpers.not as ~,
    Helpers.sub as -,
    Helpers.xor as ^
} for UD60x18 global;

to override the basic operations for a custom integer type.

1

No, Solidity doesn't support operator overloading as of Jan 2018.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.