I love Kotlin but I’m not quite sure how to properly separate concepts.
To give concrete examples:
-
Default enum value
You refer to enum values like
Enum.Value1
, how do you get a default value? It’d make sense to have something likeEnum.default
, so I write:enum class Enum { Value1, Value2; companion object { val default = Value2 } }
This creates a whole new object that I don’t need and according to other discussions I shouldn’t be using objects for namespacing. The alternative would be a top-level
EnumDefault
I guess? -
Factory methods
Similarly, there are factory methods such as
Url.fromString()
that I either have to put intocompanion object
or define at top level. -
Global constants and variables
It’s awesome when you can keep them private by sometimes you have various constants that just need to be public. And sometimes they are closely related. For instance, you could have
BUTTON_ANIMATION_DURATION
andBUTTON_ANIMATION_DELAY
. It would be nice if you didn’t have to define it like that, instead, you might want to say something likeobject ButtonAnimation { const val DURATION = 250 const val DELAY = 100 }
but then again, you created an object you don’t need, and unless you are using
const
accessingDURATION
has to go through the whole accessingButtonAnimation.INSTANCE
and callingButtonAnimation.getDURATION()
… -
Nested classes
In a similar way, you might want to have a file with classes such as
Request
andResponse
that are accesses asHandshake.Request
andHandshake.Response
from outside. You can somewhat solve this by putting them into another class, if it makes sense, or an interface.
It feels that it would be useful to allow importing packages. This would also help with conflicting import names. For instance, instead of writing
import library.widget.Preference as WidgetPreferenece
import myapp.config.Preference as ConfigPreference
you could write
import library.widget as widget
import myapp.config as config
val foo = findView<widget.Preference>()
val bar = config.Preference()
Also it would perhaps be nice to be able to write namespace ButtonAnimation {}
or something like the following, that would be used solely for keeping constants, factory methods, etc together:
enum class Enum {
Value1,
Value2;
companion namespace { // not an object!
val default = Value2
}
}