https://kotlinlang.org logo
l

LastExceed

07/11/2020, 6:34 AM
Copy code
myMutableInt = myMutableInt + 2
can be simplified to
Copy code
myMutableInt += 2
but is there any way to do something similar with non-operator functions, e.g. simplifying
Copy code
myMutableInt = myMutableInt.coerceIn(10, 100)
to something like
Copy code
myMutableInt .= coerceIn(10,100) //not valid syntax
?
i

Iaroslav Postovalov

07/11/2020, 9:06 AM
.=
operator does not exist in Kotlin.
l

LastExceed

07/11/2020, 9:06 AM
yes, thats exactly what my comment says.
i

Iaroslav Postovalov

07/11/2020, 9:07 AM
I think you may introduce an infix function.
Like
myMutableInt coerceIn (10 to 100)
However, there is no way to override any form of reassignment.
l

LastExceed

07/11/2020, 9:08 AM
so i take it that what i am looking for doesnt exist ?
i

Iaroslav Postovalov

07/11/2020, 9:08 AM
I think yes.
l

LastExceed

07/11/2020, 9:08 AM
alright ty
i

Iaroslav Postovalov

07/11/2020, 9:09 AM
I know only one language that allows to create custom operators
a

andylamax

07/11/2020, 9:55 AM
Cpp?
e

E.Kisaragi

07/11/2020, 12:54 PM
but, you can't overwrite variable like pointer.
i

Iaroslav Postovalov

07/11/2020, 1:34 PM
@andylamax ML
In C++ you only can overload operators' behavior
2 Views