{"record":{"id":"acb6a5d551887b9a","repo":"apple/pkl","slug":"pkl-function-types-are-not-supported-by-the-kotlin","errorCode":null,"errorMessage":"Pkl function types are not supported by the Kotlin code generator.","messagePattern":"Pkl function 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":751,"sourceCode":"          \"pkl.base#DurationUnit\" -> DURATION_UNIT\n          \"pkl.base#DataSizeUnit\" -> DATA_SIZE_UNIT\n          \"pkl.base#Uri\" -> URI\n          else -> {\n            val className = typeAlias.toKotlinPoetName()\n            when {\n              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)","sourceCodeStart":733,"sourceCodeEnd":769,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt#L733-L769","documentation":"KotlinCodeGenerator.toKotlinPoetName throws when a type alias resolves to a Pkl function type: KotlinPoet output for generated classes cannot represent Pkl function types, so the generator aborts instead of producing invalid Kotlin.","triggerScenarios":"A generated module declares a property or type alias whose type is a function type, e.g. `transform: (String) -> Int`.","commonSituations":"Modeling callbacks or lambdas in Pkl schemas, then generating Kotlin data classes.","solutions":["Replace the function type in the Pkl schema with a class or typealias that maps to a Kotlin type.","Exclude the offending property/type from code generation."],"exampleFix":"// before (Pkl)\ntransform: (String) -> Int\n// after\ntransformer: Transformer // a class/interface","handlingStrategy":"validation","validationCode":"fun containsFunctionType(t: PType?): Boolean =\n  t is PType.Function // extend to walk property types of module schema before codegen","typeGuard":"fun isFunctionType(t: PType?): Boolean = t is PType.Function","tryCatchPattern":"try {\n  generator.generate(module)\n} catch (e: KotlinCodeGeneratorException) {\n  if (e.message?.contains(\"function types are not supported\") == true) {\n    logger.error(\"Remove function-typed properties: ${e.message}\")\n  } else throw e\n}","preventionTips":["Do not declare function-typed properties in modules targeted by Kotlin codegen","Represent behavior via classes/interfaces, not lambdas, in generated schemas","Lint module schemas for PType.Function before CI codegen runs"],"tags":["kotlin","codegen","function-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"}