{"record":{"id":"d75314a53b7103c6","repo":"apple/pkl","slug":"pkl-union-types-are-not-supported-by-the-kotlin-co","errorCode":null,"errorMessage":"Pkl union types are not supported by the Kotlin code generator.","messagePattern":"Pkl union types are not supported by the Kotlin code generator\\.","errorType":"exception","errorClass":"KotlinCodeGeneratorException","httpStatus":null,"severity":"error","filePath":"pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt","lineNumber":757,"sourceCode":"              typeAlias.typeParameters.isEmpty() -> className\n              typeArguments.isEmpty() -> {\n                // no type arguments provided for a type alias with type parameters -> fill in\n                // `Any?` (equivalent of `unknown`)\n                val typeArgs = Array(typeAlias.typeParameters.size) { ANY_NULL }\n                className.parameterizedBy(*typeArgs)\n              }\n              else -> className.parameterizedBy(*typeArguments.toKotlinPoet())\n            }\n          }\n        }\n      is PType.Function ->\n        throw KotlinCodeGeneratorException(\n          \"Pkl function types are not supported by the Kotlin code generator.\"\n        )\n      is PType.Union ->\n        if (CodeGeneratorUtils.isRepresentableAsString(this)) STRING\n        else\n          throw KotlinCodeGeneratorException(\n            \"Pkl union types are not supported by the Kotlin code generator.\"\n          )\n\n      // occurs on RHS of generic type aliases\n      is PType.TypeVariable -> TypeVariableName(typeParameter.name)\n      else -> throw AssertionError(\"Encountered unexpected PType subclass: $this\")\n    }\n\n  private fun List<PType>.toKotlinPoet(): Array<TypeName> =\n    map { it.toKotlinPoetName() }.toTypedArray()\n\n  private val nameMapper = NameMapper(options.renames)\n}\n","sourceCodeStart":739,"sourceCodeEnd":771,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt#L739-L771","documentation":"Union types are only generated when all members are string literals (then emitted as String, via isRepresentableAsString). Any other union reaching toKotlinPoetName has no Kotlin representation and throws KotlinCodeGeneratorException.","triggerScenarios":"A property/alias typed with a non-string-literal union, e.g. `x: Int | String`, `y: A | B`, processed by Kotlin codegen.","commonSituations":"Schemas using unions for constrained numbers or mixed-type fallbacks (`String | Null` aside, typically heterogeneous unions), then generating Kotlin bindings.","solutions":["Replace the union with string literals (`typealias X = \"a\"|\"b\"`) if it represents an enumeration.","Model the union as a sealed-style set of classes or a single common supertype in Pkl.","Loosen the property to one concrete type that codegen supports."],"exampleFix":"// before (Pkl)\nvalue: Int | String\n// after\ntypealias Value = \"one\" | \"two\"\nvalue: Value","handlingStrategy":"validation","validationCode":"fun isUnionSafeForKotlin(t: PType?): Boolean =\n  t !is PType.Union || CodeGeneratorUtils.isRepresentableAsString(t)","typeGuard":"fun isKotlinUnionSafe(t: PType?): Boolean =\n  t !is PType.Union || CodeGeneratorUtils.isRepresentableAsString(t)","tryCatchPattern":"try {\n  generator.generate(module)\n} catch (e: KotlinCodeGeneratorException) {\n  if (e.message?.contains(\"union types are not supported\") == true) {\n    logger.error(\"Replace non-string union with typealias or class: ${e.message}\")\n  } else throw e\n}","preventionTips":["Express enumerations as string-literal typealiases, not arbitrary unions","Model alternatives as classes with a common supertype","Run isRepresentableAsString checks over unions in a pre-codegen lint"],"tags":["kotlin","codegen","union-type","unsupported-type"],"backgroundTag":"unsupported-operation","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}