Kotlin-native: Compiler preprocessor

Created on 5 Apr 2018  路  6Comments  路  Source: JetBrains/kotlin-native

Hi, I know we can user multiplatform project for paltform specific bits, but sometimes a #ifdef or #if os(Linux) on swift for example is just easier/faster to use than creating 3 projects for just a function

Is it possible to get something similar for kotlin native ?

Most helpful comment

Can we please have that feature? i really need it
There is no valid reason to not have it, and use cases exist that solve real world scenario

Enable feature A using one compiler preprocessor, enable debugging stuff by switching one value etc

Tooling issue is not true, C# have it and Rider handle it just fine, it's not hard to implement..

All 6 comments

Classical conditional compilation usually kills tooling, so we not planning to support such a behavior. What's the problem with file level conditional compilation, where you just select different sources for different targets, if you find MPP on overkill?

What about constexpr (as intended in C++)? Or at least some form of metaprogramming, Reified type + inline is not so far away as a concept.

hmm yes i can do that on a separate file you are right, is there a way to exclude file based on target with gradle plugin ? or any example to integrate with gradle ?

edit:
Oh i found it on doc:

konan.targets = [ 'linux', 'macbook', 'wasm32' ]

konanArtifacts {
    program('foo') {
        // This source file is used for all targets
        srcFiles 'common.kt'

        target('linux') {
            // For Linux common.kt and linux.kt will be compiled
            srcFiles 'linux.kt'
        }

        target('macbook') {
            // For MacOS common.kt and macbook.kt will be compiled
            srcFiles 'macbook.kt'
        }

        // Only common.kt will be compiled for wasm32
    }
}

This is perfect!

Another usecase for example i want some things just in debug, or if i want certain feature to be toggled

Can we please have that feature? i really need it
There is no valid reason to not have it, and use cases exist that solve real world scenario

Enable feature A using one compiler preprocessor, enable debugging stuff by switching one value etc

Tooling issue is not true, C# have it and Rider handle it just fine, it's not hard to implement..

Any kind of preprocessing adds significant challenges to source code processing. In addition to that, you lose one-to-one mapping between source code unit and translated one, as you can get different outputs from one input.

As I see it, there should be significant benefits provided by preprocessor which are hard to have otherwise. #ifdef case is very easy to cover using Strategy pattern (basically having one interface and multiple classes implementing it for different platforms, and matching these source files with corresponding platforms via Gradle). Same goes for anything else, including debug logging. People are using this all the time via build flavors in Android, for example.

Also you should realize that Kotlin will eventually have one compiler backend for all frontends, both JVM, JS and Native, so it would have to support this on JVM. That also would be significant effort for nothing.

So there are perfectly valid reasons not to have it, which overweight benefits.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alastaircoote picture alastaircoote  路  3Comments

Marcopohlo picture Marcopohlo  路  4Comments

jonnyzzz picture jonnyzzz  路  4Comments

msink picture msink  路  4Comments

9468305 picture 9468305  路  3Comments