Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin: use $default functions to implement @JvmOverloads #11105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

smowton
Copy link
Contributor

@smowton smowton commented Nov 3, 2022

This avoids extracting the default value expression in more than one place, which causes inconsistencies for e.g. anonymous classes, which expect to have a single new expression associated.

This avoids extracting the default value expression in more than one place, which causes inconsistencies for e.g. anonymous classes, which expect to have a single `new` expression associated.
@smowton smowton requested review from a team as code owners Nov 3, 2022
@github-actions github-actions bot added the Kotlin label Nov 3, 2022
Copy link
Contributor

@tamasvajk tamasvajk left a comment

What were the "inconsistencies for e.g. anonymous classes"?

@smowton
Copy link
Contributor Author

smowton commented Nov 4, 2022

@tamasvajk the isAnonymClass relation is supposed to be a bijection, meaning a one-to-one ClassInstanceExpr <-> AnonymousType relationship. If we extract the default value in both the $default function and a JvmOverloads function, then you end up with something like

fun f(x: SomeInterface = { a -> a }, y: Int)

becoming

fun f$default(var x: SomeInterface?, var y: Int?, argsMask: Int, ...) {
  if (!(argsMask & 1)) x = { a -> a }
  ...    
}

fun f(y: Int) {
  f({ a -> a }, y)
}

Thus the source-level class representing the anonymous class has been duplicated. We can handle that either by allowing that duplication and extracting two classes, or work around the problem by implementing fun f(y: Int) { f$default(null, y, 3, ...) }, which is the option I've taken here.

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

Successfully merging this pull request may close these issues.

None yet

2 participants