{"record":{"id":"4973efc4cdc27c91","repo":"apple/pkl","slug":"pkl-function-types-are-not-supported-by-the-java-c","errorCode":null,"errorMessage":"Pkl function types are not supported by the Java code generator.","messagePattern":"Pkl function types are not supported by the Java code generator\\.","errorType":"exception","errorClass":"JavaCodeGeneratorException","httpStatus":null,"severity":"error","filePath":"pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt","lineNumber":887,"sourceCode":"          else -> {\n            if (CodeGeneratorUtils.isRepresentableAsEnum(aliasedType, null)) {\n              if (typeAlias.isStandardLibraryMember) {\n                throw JavaCodeGeneratorException(\n                  \"Standard library typealias `${typeAlias.qualifiedName}` is not supported by Java code generator. \" +\n                    \"If you think this is an omission, please let us know.\"\n                )\n              } else {\n                // reference generated enum class\n                typeAlias.toJavaPoetName().nullableIf(nullable)\n              }\n            } else {\n              // inline type alias\n              aliasedType.toJavaPoetName(nullable)\n            }\n          }\n        }\n      is PType.Function ->\n        throw JavaCodeGeneratorException(\n          \"Pkl function types are not supported by the Java code generator.\"\n        )\n      is PType.Union ->\n        if (CodeGeneratorUtils.isRepresentableAsString(this)) STRING.nullableIf(nullable)\n        else\n          throw JavaCodeGeneratorException(\n            \"Pkl union types are not supported by the Java code generator.\"\n          )\n      else ->\n        // should never encounter PType.TypeVariableNode because it can only occur in stdlib classes\n        throw AssertionError(\"Encountered unexpected PType subclass: $this\")\n    }\n\n  private fun TypeName.nullableIf(isNullable: Boolean): TypeName =\n    if (isPrimitive && isNullable) box()\n    else if (isPrimitive || isNullable) this else annotated(nonNullAnnotation)\n\n  private fun TypeName.boxIf(shouldBox: Boolean): TypeName = if (shouldBox) box() else this","sourceCodeStart":869,"sourceCodeEnd":905,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-codegen-java/src/main/kotlin/org/pkl/codegen/java/JavaCodeGenerator.kt#L869-L905","documentation":"JavaCodeGenerator.toJavaPoetName throws JavaCodeGeneratorException when the type being mapped resolves to a Pkl function type: javaPoet has no representation for function types in generated classes, so codegen refuses the input schema rather than emitting invalid Java.","triggerScenarios":"A Pkl module declares a property, parameter, or type alias whose type is a function type, and that module is fed to the Java code generator.","commonSituations":"Modules exposing callback-style properties or functional parameters, or typealiases like `type Handler = (String) -> Boolean` appearing in a schema meant for codegen.","solutions":["Replace the function type in the Pkl schema with a concrete class or type alias that maps to a Java type.","Exclude the offending property/type from code generation and hand-write the mapping."],"exampleFix":"// before (Pkl)\ntransform: (String) -> String\n// after\nclass Transform { mode: String }","handlingStrategy":"validation","validationCode":"fun hasFunctionType(module: Module) = module.declarations.any { it.type is PType.Function }","typeGuard":"fun PType.isFunctionType() = this is PType.Function","tryCatchPattern":"try { generate() } catch (e: JavaCodeGeneratorException) { if (\"function types\" in e.message!!) stripFunctionalMembers() else throw e }","preventionTips":["Keep function-typed members out of codegen-targeted modules","Model behavior as data (strategy classes/unions) instead","Separate runtime-only functional APIs from the generated schema surface"],"tags":["codegen","function-type","unsupported"],"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"}