Some misunderstanding of type infer ```private fun...
# kotlin-native
r
Some misunderstanding of type infer
Copy code
private fun getFile(name: String) {
        val file = fopen(name, "wt")
        if (file == null) throw Error("Cannot write file '$name'")
        return file

private fun writeM4() {
        val file = getFile("config.m4")
        fputs(m4Content, file)
...
    }
Compiler is fail with errors
Copy code
dsl.kt:77:30: error: type mismatch: inferred type is Unit but CValuesRef<FILE /* = _IO_FILE */>? was expected
            fputs(m4Content, file)
                             ^
dsl.kt:86:16: error: type mismatch: inferred type is CPointer<FILE /* = _IO_FILE */>? but Unit was expected
        return file
Why it does not infer automatically?
r
Add the return type to
getFile()
. Return type always needs to be explicit in a function with a block body.
o
Sure, your getFile signature assumes Unit return type, make it return FILE
r
In JVM Kotlin return type inferred automatically. Nikolay, may be you know, JetBrains have plans to add this behavior?
o
it is inferred automatically in both Kotlin/JVM and Kotlin/Native, your code just explicitly says (by omitting return type) that it returns Unit: https://kotlinlang.org/docs/reference/functions.html#unit-returning-functions
r
Indeed, I'm sorry