Kotlin / kotlin-spec

Kotlin Language Specification:
https://kotlinlang.org/spec
Apache License 2.0
393 stars 80 forks source link

describe default value evaluation #67

Closed jbaron closed 3 years ago

jbaron commented 3 years ago

Right now the spec doesn't specify when default values are actually evaluated. Would be nice to highlight that this is the case every time when a default value is required, but not in other cases.

So explain that someFunction is invoked every time no value for the parameter is provided and in other cases it is not invoked (so no overhead).

 fun test(a:Int = someFunction()) {
  }

This might look like an obvious clarification, but different languages solve this in different ways. So might take away some doubt depending where you coming from.

ice-phoenix commented 3 years ago

Hi mate, thanks for your suggestion! The evaluation order (and that the default values are reevaluated on every function call) is actually specified in the function call expression section. To clarify, we will add the forward link to it from the function declaration section and also add a separate note about it.

Cheers!