0
votes

The Reified Type parameters support run-time access to types passed to functions. I understand that this can be useful in certain scenarios to avoid reflection.

But there are examples of creating extension functions with reified type parameters which simply wrap T::class.java syntax in a method like below.

  inline fun <reified T > Context.systemService() =
      ContextCompat.getSystemService(this,T::class.java)

The kotlin reference mentions for the below usage the call-site is not pretty. Why is the following usage discouraged?

  ContextCompat.getSystemService(this, NotificationManager::class.java)

Instead we can now write it like this :

  systemService<NotificationManager>()

Are there any other benefits in such a scenario except that the code looks cleaner?

Note: the example is from I/O' 18

1
They both do the same thing, it doesn't matter which you callZoe

1 Answers

2
votes

This is entirely up to personal opinion, as both functions will do the same thing.

People will tend to consider the reified Kotlin extension method more idiomatic because it makes use of advanced language feature to provide simpler syntax than what you'd otherwise have to use. Doesn't mean you absolutely have to use it - decide if you like it for yourself.