https://kotlinlang.org logo
s

sanf0rd

03/12/2018, 3:50 PM
Exactly. My point is to make
when()
with enums as much expressive as it is with a
sealed class
r

rrader

03/12/2018, 3:51 PM
you can use static import
s

sanf0rd

03/12/2018, 3:53 PM
I can, but as @benleggiero said it could have naming conflicts
r

rrader

03/12/2018, 3:54 PM
and for 0.09% cases you can use full name Enum.VALUE
👎🏼 1
👍 1
when last time did you got a conflict on names?
👍 1
👎🏼 1
s

sanf0rd

03/12/2018, 4:00 PM
I’m just arguing that it should have a proper compile inference. Since kotlin already has a well-known type inference in other aspects of the language. And from where did you get the 0.09% magic number?
b

benleggiero

03/12/2018, 4:16 PM
I can give examples of naming conflicts no problem.
Copy code
sealed 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:
Copy code
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 :P
@rrader I think you mean
com.company.project.subpackage.SomeVeryDescriptiveEnumClassName.VALUE_NAME
g

gildor

03/12/2018, 4:22 PM
Agree, realistic case if your code uses Sealed classes and Enums a lot. Practically I never had this problem, but I can imagine such cases
b

benleggiero

03/12/2018, 6:27 PM
@gildor having gotten used to this in Swift, I try to use it in Kotlin a lot and rub into naming conflicts
Here's another example that just came to mind last night when working with UIs: I have three classes named
Point
,
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 imports
👍 1
s

sanf0rd

03/13/2018, 4:54 PM
I’m facing one right now. I have one FacebookLogin observer returning a
Loading
subtype of
sealed class FacebookStatus
and
fun changeViewState()
also requires another
Loading
subtype of
sealed class NewUserViewMode
.
👍🏼 1
had to rename the last one to LoadingState. =/
b

benleggiero

03/17/2018, 6:29 PM
At this point it feels like going the Objective-C route of naming it
FacebookStatusLoading
and
NewUserViewModeLoading
would be better.... not a good day when I say "Objective-C's way of doing things would be better" 😕