Comment on page
Compiler Type Inference
The primary focus of Konsist API is to reflect the state of the Kotlin source code (that will be verified), not the state of the running program. The Kotlin compiler has a deeper understanding of the Kotlin code base than Konsist, because the compiler can infer more information. Let's take a look at a few examples.
Consider this class:
public? Yes obviously it is
public, however, the
hasPublicModifiermethod returns the
koClass.hasPublicModifier() // false
Why is that? The
publicvisibility modifier is the default visibility modifier in Kotlin. Meaning that class will be
publiceven if it does not have the explicit
publicmodifier. Since the class has no
hasPublicModifiermethod returns false. To distinguish between class being
publicand class heaving explicit
publicmodifier Konsist API provides another method to retrieve declaration visibility:
koClass.isPublicOrDefault() // true
Let's look at the
private val name = String
nameproperty is obviously of the
Stringtype is inferred, so Konsist has no way of Knowing the actual type (in this exact case this is achievable, but with more complex expressions containing delegates, setters, getters, or methods this approach would not work).
Let's look at the primary constructor for the same class:
Loggerthe class has a primary constructor because the Kotlin compiler will generate a parameterless constrictor under the hood. However, the Konsist API will return a
nullvalue because the primary constructor is not present in the Kotlin source code:
koClass.primaryConstructor // null
Consider this function:
fun getName() = "Konsist"
Kotlin will infer
Stringas the return type of the
getNamefunction. Since the source code does not contain this explicit return type Konsist lacks information about the return type. In this scenario,
hasReturnTypethe method will return the
koFunction.hasReturnType() // false
Unlike the previous example, Konsist has no way to determine the actual function return type.