Infix Functions
As the chapter on operator overloading mentioned, you cannot implement your own operators… at least not in the form of punctuation. And, we saw that some operators, like in
, are not based on punctuation.
The nice thing is that Kotlin lets you create your own “operators”, to support syntax like "this is very impolite" disemvowel "*"
.
Postfix and Infix
Fans of classic HP calculators will remember “reverse Polish notation” (RPN) as the data entry format. To calculate 2 plus 2, you would press 2 Enter 2 + on the keyboard. The Enter key pushes a number onto the stack, and the + operation takes the current number, pops the last number off of the stack, and adds them. This is called “postfix” notation.
Calculators from other manufacturers often use an = key. There, to calculate 2 plus 2, you would press 2 + 2 =. This “infix” notation is also how most programming languages work (Forth being one exception).
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.