Local lazy variables transpile to broken Kotlin
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 3.2k
- Forks
- 106
- Avg merge
- 6d 13h
- Merged PRs (30d)
- 1
Description
I noticed the problem after creating a lazy var inside a function.
Swift:
func example() {
lazy var lazyVariable = { return Int.random(in: 1...10) }
print(lazyVariable)
}
Transpiler created Kotlin code:
internal open fun example() {
var lazyVariable = l@{ -> return@l Int.random(in_ = 1..10) }
private var lazyVariableinitialized = false
print(lazyVariable)
}
Which results in error:
Modifier 'private' is not applicable to 'local variable'
Do you think you could improve your logic for creating lazy variables equivalents in Kotlin so they could also work in function scope, not only the class scope?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the Swift function example and tracing the transpiler logic that creates Kotlin equivalents for lazy variables. Done means the generated local Kotlin declaration compiles without an invalid private modifier and preserves the lazy variable behavior in function scope.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, swift
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100