Skip to content
This repository has been archived by the owner on Oct 16, 2018. It is now read-only.

Prefer parameters with default values last in the parameter list. #57

Open
NightlyNexus opened this issue Jan 26, 2018 · 4 comments
Open

Comments

@NightlyNexus
Copy link

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)

@JakeWharton
Copy link
Contributor

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.

@NightlyNexus
Copy link
Author

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

@JakeWharton
Copy link
Contributor

JakeWharton commented Jan 26, 2018 via email

@NightlyNexus
Copy link
Author

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 = {}).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants