Unary operators

Unary operators don't have parameters and act directly in the dispatcher.

We can add a not operator to the Wolf class:

operator fun Wolf.not() = "$name is angry!!!"

!talbot // talbot.not()

The following table will show you all the possible unary operators that can be overloaded:

Operator

Equivalent

Notes
+x x.unaryPlus()
-x x.unaryMinus()
!x x.not()
x++ x.inc() Postfix, it must be a call on a var, should return a compatible type with the dispatcher type, shouldn't mutate the dispatcher.
x-- x.dec() Postfix, it must be a call on a var, should return a compatible type with the dispatcher type, shouldn't mutate the dispatcher.
++x x.inc() Prefix, it must be a call on a var, should return a compatible type with the dispatcher type, shouldn't mutate the dispatcher.
--x x.dec() Prefix, it must be a call on a var, should return a compatible type with the dispatcher type, shouldn't mutate the dispatcher.

 

Postfix (increment and decrement) returns the original value and then changes the variable with the operator returned value. Prefix returns the operator's returned value and then changes the variable with that value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.39