One-line function call on optional value yields Kotlin compile error: Type mismatch: inferred type is Unit? but Unit was expected
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 3.2k
- Forks
- 106
- Avg merge
- 6d 13h
- Merged PRs (30d)
- 1
Description
If a Void function has a single-line that invokes another Void function, we transpile it into a single line like: fun x(): Unit = f(), but that raises a compile error because f() returns Unit?.
For example, the Swift:
class Thing {
var t: Thing? = nil
init() {
}
func go() {
}
func optionalCall() {
t?.go()
}
}
transpiles into the Kotlin:
internal open class Thing {
internal open var t: Thing? = null
internal constructor() {
}
internal open fun go() = Unit
internal open fun optionalCall(): Unit = t?.go()
}
which yields the compile error:
Type mismatch: inferred type is Unit? but Unit was expected
The only workaround is to add another line to the function, like:
func optionalCall() {
let x = t?.go()
_ = x
}
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 from the transpilation path for Swift Void functions whose single statement is an optional call, using the Thing example to reproduce the generated Kotlin and compiler error. Verify the fix by checking that optionalCall produces Kotlin accepted by the compiler without requiring an extra statement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, swift
- Domain
- compilers, mobile-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100