kotlin-guides icon indicating copy to clipboard operation
kotlin-guides copied to clipboard

Prefer parameters with default values last in the parameter list.

Open NightlyNexus opened this issue 7 years ago • 4 comments
trafficstars

https://android.github.io/kotlin-guides/interop.html#function-overloads-for-defaults mentions this but in a different context.

This is useful even in pure Kotlin, regardless of Java interop because it doesn't require naming the other parameters at the call site.

data class Pizza(val cheese: Boolean, val toppings: List<String> = listOf()) Pizza(true)

instead of

data class Pizza(val toppings: List<String> = listOf(), val cheese: Boolean) Pizza(cheese = true)

NightlyNexus avatar Jan 26 '18 19:01 NightlyNexus

We can't add this to the style guide because I don't think we can make it a hard rule. But it's a good candidate for a hopefully forthcoming best practices / patterns section.

JakeWharton avatar Jan 26 '18 19:01 JakeWharton

truuuuue. (feel free to close if this isn't the right place for a best practices request. looking forward to that!)

NightlyNexus avatar Jan 26 '18 21:01 NightlyNexus

Let's start accumulating issues to track its contents. The problem was we didn't have enough content to warrant a tab when these guides were created.

On Fri, Jan 26, 2018 at 4:17 PM Eric Cochran [email protected] wrote:

truuuuue. (feel free to close if this isn't the right place for a best practices request. looking forward to that!)

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/android/kotlin-guides/issues/57#issuecomment-360906557, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEdkoBlh1FEPDrOuns5q0R4KDu3FWks5tOkDagaJpZM4Ruyxb .

JakeWharton avatar Jan 26 '18 21:01 JakeWharton

One notable exception is lambdas. Lambdas are a higher priority for being last in the parameter list. fun foo(unused: Any? = null, a: (String) -> Unit) {} foo {} works here without needing to name the a parameter. That is, no need for foo(a = {}).

NightlyNexus avatar Feb 24 '18 21:02 NightlyNexus