I know that many language, C++, Python etc, support operator overloading, and we can redefine standart operators like +. But if we need add new operator?
-
What "operator" would you like to add? – Jérémi Panneton Jul 17 '17 at 15:24
-
In many languages that simply isn't possible without an outside library (I know this to be true for C++). I wonder, then, if a new operator should instead just be a function. You may find more on this here: https://stackoverflow.com/questions/8425077/can-i-create-a-new-operator-in-c-and-how – Reticulated Spline Jul 17 '17 at 15:24
-
3Why you downvote question without any comment? – Dmitry Sokolov Jul 17 '17 at 15:24
-
2So we can make the `<3` operator? :) – Grimmy Jul 17 '17 at 15:26
-
What is your question? How to define a new operator in `c++`? In `python`? Why is this question tagged `math`? – François Andrieux Jul 17 '17 at 15:27
-
If you're bored you can write an operator based programming language,.. if you do I'd be glad to help ;) – Maaaaa Jul 17 '17 at 15:30
-
Different languages support operator overloading in different ways. Depending on the language, and what operator you need to "add", the spectrum runs from "impossible" to "possible". – Peter Jul 17 '17 at 15:30
-
A major problem would be to define the precedence of your new operator relatively to the standard ones, and the rules for associativity. Unless the language explicitely gives you control over this, you're out of luck. – Thierry Lathuille Jul 17 '17 at 16:30
-
This question is a moving target without an objective. Maybe you are really looking for [Functional Programing](https://en.wikipedia.org/wiki/Functional_programming)? Please update your question. – lakeweb Jul 17 '17 at 19:40
-
I guess you want to run some experiments and that's why you don't have a precise description. If so, try Smalltalk; it will not limit your thinking. – Leandro Caniglia Jul 18 '17 at 02:19
2 Answers
Neither C++ nor Python (C++, Python) support the overloading of "new" operators (although C++ does support overloading the new
operator). Overloading operators is just syntactic sugar though - you can always just define a member function, name it something that explains its behaviour better than a single character operator could, and call it instead.
There do exist languages where this is possible: for example Haskell, a beautiful language that does allow you to define your own operators, by using symbols for the name and surrounding them with parentheses, eg:
(!!!) :: [a] -> Integer -> a
(x:xl) !!! 0 = x
(_:xl) !!! n = xl !!! (n-1)
_ !!! _ = error "Index out of bounds"
(note this is just a redefinition of the standard !!
function, I'm unimaginative...)

- 5,860
- 2
- 27
- 49
-
1Does this work with unicode symbols? I really want to write something like this: foo = a ⛷ b – Maaaaa Jul 17 '17 at 15:43
-
@Maaaaa Depends on if the interpreter supports Unicode - an online one I just found [here](http://rextester.com/l/haskell_online_compiler) doesn't, but GHCi might (can't test at the moment). – hnefatl Jul 17 '17 at 15:45
-
I don't think there is any language with the feature. But you can always create your own interpreter or something similar in order to process your custom code and transform it into code of the desired language. As the simplest variant, you can write a tool(or even extension for IDE), which will find something like
return_type operator$(typename arg1, typename arg2);
and insert something like
return_type reloaded_operator_dollar(typename arg1, typename arg2);
in its place. The same goes for transforming
auto result = arg1 $ arg2;
into
auto result = reloaded_operator_dollar(arg1, arg2);
upd: It seems, there are such feature in FORTH and Haskell. Thanks to Neil Butterworth for the information.

- 522
- 1
- 6
- 19
-
2
-
https://stackoverflow.com/questions/2210854/can-you-define-your-own-operators-in-f Seems F# too. But both is functional. – Dmitry Sokolov Jul 17 '17 at 15:56
-
@DmitrySokolov it seems more like simple overloading. Because only symbols `!%&*+-./<=>@^|~` can start an operator. In other words, it won't be quite "new". – John Cvelth Jul 17 '17 at 16:01