Quillraven / Fleks

Fast, lightweight, multi-platform entity component system in Kotlin
MIT License
180 stars 20 forks source link

update `EntityComparator` to be a functional interface that extends `Comparator<Entity>` #101

Closed aSemy closed 1 year ago

aSemy commented 1 year ago

update EntityComparator to be a functional interface that extends Comparator<Entity>

Using a functional interface means defining a new instance is shorter, and extending Comparator<> helps interoperability with the standard library (as demonstrated in EntityComparatorTests#useEntityComparatorToSortRegularList()).

In order to avoid the warning The corresponding parameter in the supertype 'Comparator' is named 'a'. This may cause problems when calling this function with named arguments. I renamed the parameters to match those in Comparator<>

Also, fix UNCHECKED_CAST in compareEntityBy() by adding more type restrictions

Quillraven commented 1 year ago

Did not know about functional interfaces. I heard/read "SAM" from time to time but always thought it is something completely different.

Nice to know about that feature and that it helps again to simplify the code and make it more readable. Cool PR!

Quillraven commented 1 year ago

Please have a look at the error of the workflow. Seems related to the IR browser backend? Do you have access to the build details?

aSemy commented 1 year ago

Please have a look at the error of the workflow. Seems related to the IR browser backend? Do you have access to the build details?

Hmmm strange! I'll take a look. I encountered this error yesterday, but I thought it was related to some of the changes in #99.

https://youtrack.jetbrains.com/issue/KT-59858/Kotlin-Native-Compilation-failed-Sequence-contains-more-than-one-matching-element

> Task :compileTestKotlinNative
e: Compilation failed: Backend Internal error: Exception during IR lowering

File being compiled: /home/runner/work/Fleks/Fleks/src/commonMain/kotlin/com/github/quillraven/fleks/system.kt
The root cause java.lang.IllegalArgumentException was thrown at: org.jetbrains.kotlin.backend.konan.lower.FunctionReferenceLowering$FunctionReferenceBuilder.buildClass(FunctionReferenceLowering.kt:626)

 * Source files: 
 * Compiler version info: Konan: 1.8.22 / Kotlin: 1.8.22
 * Output kind: PROGRAM

e: org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: /home/runner/work/Fleks/Fleks/src/commonMain/kotlin/com/github/quillraven/fleks/system.kt
The root cause java.lang.IllegalArgumentException was thrown at: org.jetbrains.kotlin.backend.konan.lower.FunctionReferenceLowering$FunctionReferenceBuilder.buildClass(FunctionReferenceLowering.kt:626)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:253)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:237)
    at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:68)
    at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:55)
    at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:41)
    at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
    at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
    at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
    at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
    at org.jetbrains.kotlin.backend.konan.ToplevelPhasesKt$dependenciesLowerPhase$1.invoke(ToplevelPhases.kt:220)
    at org.jetbrains.kotlin.backend.konan.ToplevelPhasesKt$dependenciesLowerPhase$1.invoke(ToplevelPhases.kt:205)
    at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
    at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
    at org.jetbrains.kotlin.backend.konan.driver.PortingFromStaticDriverKt.runPhaseInParentContext(PortingFromStaticDriver.kt:32)
    at org.jetbrains.kotlin.backend.konan.driver.phases.TopLevelPhasesKt.runBackendCodegen(TopLevelPhases.kt:153)
    at org.jetbrains.kotlin.backend.konan.driver.phases.TopLevelPhasesKt.runLowerAndCompile(TopLevelPhases.kt:139)
    at org.jetbrains.kotlin.backend.konan.driver.phases.TopLevelPhasesKt$runBackend$1$1.invoke(TopLevelPhases.kt:57)
    at org.jetbrains.kotlin.backend.konan.driver.phases.TopLevelPhasesKt$runBackend$1$1.invoke(TopLevelPhases.kt:54)
    at org.jetbrains.kotlin.backend.konan.driver.phases.TopLevelPhasesKt.processModuleFragments(TopLevelPhases.kt:116)
    at org.jetbrains.kotlin.backend.konan.driver.phases.TopLevelPhasesKt.runBackend(TopLevelPhases.kt:54)
    at org.jetbrains.kotlin.backend.konan.driver.DynamicCompilerDriver.produceBinary(DynamicCompilerDriver.kt:118)
    at org.jetbrains.kotlin.backend.konan.driver.DynamicCompilerDriver.access$produceBinary(DynamicCompilerDriver.kt:20)
    at org.jetbrains.kotlin.backend.konan.driver.DynamicCompilerDriver$run$1$1$1.invoke(DynamicCompilerDriver.kt:27)
    at org.jetbrains.kotlin.backend.konan.driver.DynamicCompilerDriver$run$1$1$1.invoke(DynamicCompilerDriver.kt:25)
    at org.jetbrains.kotlin.backend.konan.driver.PhaseEngine$Companion$startTopLevel$topLevelPhase$1.phaseBody(Machinery.kt:100)
    at org.jetbrains.kotlin.backend.konan.driver.PhaseEngine$Companion$startTopLevel$topLevelPhase$1.phaseBody(Machinery.kt:94)
    at org.jetbrains.kotlin.backend.common.phaser.SimpleNamedCompilerPhase.phaseBody(CompilerPhase.kt:207)
    at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
    at org.jetbrains.kotlin.backend.konan.driver.PhaseEngine$Companion.startTopLevel(Machinery.kt:[107](https://github.com/Quillraven/Fleks/actions/runs/5443143747/jobs/9917166962#step:6:108))
    at org.jetbrains.kotlin.backend.konan.driver.DynamicCompilerDriver.run(DynamicCompilerDriver.kt:25)
    at org.jetbrains.kotlin.backend.konan.KonanDriver.run(KonanDriver.kt:78)
    at org.jetbrains.kotlin.cli.bc.K2Native.runKonanDriver(K2Native.kt:[133](https://github.com/Quillraven/Fleks/actions/runs/5443143747/jobs/9917166962#step:6:134))
    at org.jetbrains.kotlin.cli.bc.K2Native.doExecute(K2Native.kt:70)
    at org.jetbrains.kotlin.cli.bc.K2Native.doExecute(K2Native.kt:36)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)