kotlinx-datetime icon indicating copy to clipboard operation
kotlinx-datetime copied to clipboard

Don't typealias Month (and others)

Open fluidsonic opened this issue 4 years ago • 5 comments
trafficstars

I suggest to not typealias anything in a multiplatform date/time library.

  • It mixes platform-provided API into the library's API which causes confusion and is platform-dependent.
  • It doesn't allow for adding a companion and thus "static" functionality - not even through extensions.

For example: I had code in the back-end that used Month.of(someInt). The code was relatively simple and there were no JVM-specific imports. So I've moved it to a shared multiplatform dependency and was surprised that Month.of() was no longer available. Month.<autocomplete> didn't provide me with any useful alternative.

After Cmd-clicking on Month I've learned that it is merely a typealias on JVM. It took a while to figure out that there's an additional Month(Int) function in kotlinx-datetime that I should've used instead. Scenarios like that can easily lead to confusion and inconsistency.

fluidsonic avatar Jan 31 '21 18:01 fluidsonic

Another example for Android

import android.content.Context
import kotlinx.datetime.DayOfWeek

fun DayOfWeek.toString(context: Context): String{
    return when(this){
        java.time.DayOfWeek.MONDAY -> TODO() <---- Field requires API Level 26
        java.time.DayOfWeek.TUESDAY -> TODO()
        java.time.DayOfWeek.WEDNESDAY -> TODO()
        java.time.DayOfWeek.THURSDAY -> TODO()
        java.time.DayOfWeek.FRIDAY -> TODO()
        java.time.DayOfWeek.SATURDAY -> TODO()
        java.time.DayOfWeek.SUNDAY -> TODO()
    }
}

september669 avatar Mar 17 '21 11:03 september669

@september669 the whole library requires coreLibraryDesugaring to be enabled enabling the use of java.time on previous API levels.

JakeWharton avatar Mar 17 '21 13:03 JakeWharton

@september669 the whole library requires coreLibraryDesugaring to be enabled enabling the use of java.time on previous API levels.

Thanks a lot!

september669 avatar Mar 17 '21 14:03 september669

I actually like the typealiasing as when we further process the data in the android project, we'd need need to create a wrapper that does the mapping. We actually use a expect typealias LocalDate function to expose public properties coming from our library surface and actual implement it as javas LocalDate on android and NSDateComponents for ios.

The argument with the factories will go away when the namespace feature arrives.

PaulWoitaschek avatar Apr 15 '21 09:04 PaulWoitaschek

With Kotlin 1.5 it became super annoying and really needs to be changed.

image

fluidsonic avatar May 25 '21 14:05 fluidsonic