Created
August 4, 2024 09:18
-
-
Save shangeethsivan/d9dd52d850b60c67b06c3f32827d9b84 to your computer and use it in GitHub Desktop.
Operator Overloading in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val state = State.DataLoaded | |
val newState = state + UserInputs.OnRetryClick | |
println(newState) | |
} | |
sealed class State{ | |
abstract operator fun plus(userInputs: UserInputs): State | |
object DataLoaded : State() { | |
override fun plus(userInputs: UserInputs): State { | |
return when (userInputs) { | |
UserInputs.OnRetryClick -> { | |
LoadingNewData | |
} | |
} | |
} | |
} | |
object LoadingNewData: State(){ | |
override fun plus(userInputs: UserInputs): State { | |
TODO("Not yet implemented") | |
} | |
} | |
} | |
sealed class UserInputs{ | |
object OnRetryClick: UserInputs() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment