sanf0rd
03/12/2018, 3:50 PMwhen()
with enums as much expressive as it is with a sealed class
rrader
03/12/2018, 3:51 PMsanf0rd
03/12/2018, 3:53 PMrrader
03/12/2018, 3:54 PMsanf0rd
03/12/2018, 4:00 PMbenleggiero
03/12/2018, 4:16 PMsealed class NetworkOperationResult {
class success(body: String): NetworkOperationResult()
class failure(error: Throwable?): NetworkOperationResult
}
sealed class DriveWriteResult {
object success: DriveWriteResult()
object notEnoughSpace: DriveWriteResult()
object insufficientPermissions: DriveWriteResult()
object nonexistentDirectory: DriveWriteResult()
class unknownFailure(error: Throwable): DriveWriteResult()
}
Usage:
var netResult = myFancyNetworkOperation()
when (netResult) {
is success -> print(netResult.body) // error
is failure -> showAlert("Failure due to ${netResult.error ?: "an unknown error"}")
}
val writeResult = myFancyDriveWrite()
if (writeResult is success) { // error
return
}
else {
showAlert("Could not save: ${writeResult}")
}
Written on my phone, so please forgive syntax errors :Pcom.company.project.subpackage.SomeVeryDescriptiveEnumClassName.VALUE_NAME
gildor
03/12/2018, 4:22 PMbenleggiero
03/12/2018, 6:27 PMPoint
, Size
, and Rect
. Each has a zero
value in its companion object, for the coordinate (0,0)
, the size 0×0
, and the rectangle with origin (0,0)
and size 0×0
. IME distinguishes these easily, but not static importssanf0rd
03/13/2018, 4:54 PMLoading
subtype of sealed class FacebookStatus
and fun changeViewState()
also requires another Loading
subtype of sealed class NewUserViewMode
.benleggiero
03/17/2018, 6:29 PMFacebookStatusLoading
and NewUserViewModeLoading
would be better.... not a good day when I say "Objective-C's way of doing things would be better" 😕