Konsist
GitHubSlack (kotlinlang)Twitter
  • 🚀GETTING STARTED
    • What is Konsist?
    • Getting Started
      • Add Konsist Dependency
      • Create First Konsist Test - Declaration Check
      • Create Secound Konsist Test - Architectural Check
    • Articles & Videos
  • ✅WRITING TESTS
    • Create The Scope
    • Declaration Filtering
    • Declaration Assertion
    • Architecture Assertion
    • Suppress Konsist Test
  • ✏️VERYFYING CODEBASE
    • Verify Classes
    • Verify Interfaces
    • Verify Functions
    • Verify Properties
    • Verify Generics
    • Verify Source Declarations
  • 📗FEATURES
    • Add Konsist Existing To Project (Baseline)
    • Debug Konsist Test
    • Declaration
    • Declaration Vs Property
    • Compiler Type Inference
    • Package Wildcard
    • Declaration References
    • Indirect Parents
    • Kotest Support
  • 🔍INSPIRATION
    • Starter Projects
    • Snippets
      • General Snippets
      • Android Snippets
      • Spring Snippets
      • Test Snippets
      • JUnit Snippets
      • Kotest Snippets
      • Architecture Snippets
      • Clean Architecture Snippets
      • Kotlin Serialization Snippets
      • Library Snippets
      • Generic Types Snippets
  • 🎓ADVANCED
    • Isolate Konsist Tests
    • Enable Full Command Line Logging
    • Dynamic Konsist Tests
      • Explicit Test Names
    • When Konsist API Is Not Enough
    • Additional JUnit5 Setup
    • Why There Are No Pre-defined Rules?
    • Konsist Snapshots
  • ❓HELP
    • Getting Help
    • Known Issues
      • java.lang.OutOfMemoryError: Java heap space
    • Compatibility
  • ℹ️OTHER
    • Changelog
    • Project Status
    • Contributing
    • Contributors
    • Assets And Logos
    • Open Source Licenses
    • Sponsor Konsist
Powered by GitBook
On this page
  • Class Example
  • Property Example
  • Primary Constructor Example
  • Function Return Type Example
Edit on GitHub
Export as PDF
  1. FEATURES

Compiler Type Inference

PreviousDeclaration Vs PropertyNextPackage Wildcard

Last updated 11 months ago

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.

Class Example

Consider this class:

class Logger

Is Logger class public? Yes obviously it is public, however, the hasPublicModifier method returns the false value:

koClass.hasPublicModifier() // false

Why is that? The public visibility modifier is the default visibility modifier in Kotlin. Meaning that class will be public even if it does not have the explicit public modifier. Since the class has no public modifier the hasPublicModifier method returns false. To distinguish between class being public and class having explicitpublic modifier Konsist API provides another method to retrieve declaration visibility:

koClass.isPublicOrDefault() // true

Property Example

Let's look at the name property:

private val name = String

The name property is obviously of the String type. However String type 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).

The may enable this feature for Konsist.

Primary Constructor Example

Let's look at the primary constructor for the same class:

class Logger

The Logger the class has a primary constructor because the Kotlin compiler will generate a parameterless constructor under the hood. However, the Konsist API will return a null value because the primary constructor is not present in the Kotlin source code:

koClass.primaryConstructor // null

Function Return Type Example

Consider this function:

fun getName() = "Konsist"

Kotlin will infer String as the return type of the getName function. Since the source code does not contain this explicit return type Konsist lacks information about the return type. In this scenario, hasReturnType the method will return the false value:

koFunction.hasReturnType() // false

Unlike the previous example, Konsist has no way to determine the actual function return type.

📗
K2 Compiler plugin