-
Notifications
You must be signed in to change notification settings - Fork 14
Using inline functions #16
Comments
I'd make a single exception for extensions meant to implement an // PointF is likely to be used in hot code paths for drawing
// and operator use will be compiled down to a field access
// rather than static method invocation
@Suppress("NOTHING_TO_INLINE")
inline operator fun PointF.component1() = x
@Suppress("NOTHING_TO_INLINE")
inline operator fun PointF.component2() = y |
Also, factory functions using reified types only to access a fun complicatedFunction(clazz: Class<*>) {
// don't need a reified type for any work done here
// ...
}
inline fun <reified T : Any> complicatedFunction() = complicatedFunction(T::class.java) Of course if you need the reified type for more than just a |
One other thought that deserves its own issue (but I'll post here first, for the record): What's the preference between using class Foo <T: Any>(val clazz: Class<T>) {
companion object {
inline operator fun <reified T : Any> invoke() = Foo(T::class.java)
}
} |
One more question on this: What about fun <K : Any, V : Any> lruCache(maxSize: Int, create: (K) -> V) = object : LruCache<K, V>(maxSize) {
override fun create(key: K): V = create(key)
} This type of factory function in itself may be another issue, but should this function be |
Functions should only be made
inline
when they useinline
-only features like inlined lambda parameters or reified types.Inline functions should generally be short as they increase the code size on every invocation.
When an inline function that uses a reified type paremeter tends to become too long, consider extracting a part of the body into a separate, non-
inline
function. Example:When a class constructor expects a parameter of type
Class<T>
orKClass<T>
, consider making aninline
factory method so that the parameter can be omitted. Example:The text was updated successfully, but these errors were encountered: