booleankotlinkotlin-extension

Boolean extension function


When I try to create Extension Function to set Boolean true or false like the below.

Boolean.setTrue(){
 this = true
}

Boolean.setFalse(){
 this = false
}

It says variable expected. How to achieve this.


Solution

  • You cannot change the value of this, this would break a lot of assumptions, even if you could you would not be able to change the value, as Booleans are immutable.

    More generally, there is a fine line between simplifying code, and making it more complex, and in this case that would complicate it. I would agree that adding String.splitByDot() may make sense, but replacing idiomatic code tends to just make the code more complex, as you start to wonder why the code had to be replaced.