{"record":{"id":"18dd64b5f09dcc1d","repo":"apple/pkl","slug":"cannot-convert-pkl-base-int-value-to-kotlin-uby","errorCode":null,"errorMessage":"Cannot convert pkl.base#Int `$value` to kotlin.UByte because it is outside range `0...$max`","messagePattern":"Cannot convert pkl\\.base#Int `\\$value` to kotlin\\.UByte because it is outside range `0\\.\\.\\.\\$max`","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/mapper/KotlinConversions.kt","lineNumber":65,"sourceCode":"    }\n\n  val pIntToUShort: Conversion<Long, UShort> =\n    Conversion.of(PClassInfo.Int, UShort::class.java) { value, _ ->\n      val max = 0xFFFF // use literal instead of `UShort.MAX_VALUE.toLong()`\n      if (value < 0 || value > max) {\n        throw ConversionException(\n          \"Cannot convert pkl.base#Int `$value` to kotlin.UShort \" +\n            \"because it is outside range `0...$max`\"\n        )\n      }\n      value.toUShort()\n    }\n\n  val pIntToUByte: Conversion<Long, UByte> =\n    Conversion.of(PClassInfo.Int, UByte::class.java) { value, _ ->\n      val max = 0xFF // use literal instead of `UByte.MAX_VALUE.toLong()`\n      if (value < 0 || value > max) {\n        throw ConversionException(\n          \"Cannot convert pkl.base#Int `$value` to kotlin.UByte \" +\n            \"because it is outside range `0...$max`\"\n        )\n      }\n      value.toUByte()\n    }\n\n  val pStringToKotlinRegex: Conversion<String, Regex> =\n    Conversion.of(PClassInfo.String, Regex::class.java) { value, _ -> Regex(value) }\n\n  val pRegexToKotlinRegex: Conversion<Pattern, Regex> =\n    Conversion.of(PClassInfo.Regex, Regex::class.java) { value, _ -> value.toRegex() }\n\n  val all: Collection<Conversion<*, *>> =\n    Collections.unmodifiableList(\n      listOf(\n        pIntToULong,\n        pIntToUInt,","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/mapper/KotlinConversions.kt#L47-L83","documentation":"Range validation in KotlinConversions.pIntToUByte: a pkl.base#Int is converted to kotlin.UByte only if it fits 0..255 (0xFF); otherwise ConversionException is thrown. The input at fault is a negative Long or a Long above 255 used where a UByte is required.","triggerScenarios":"config.to<UByte>() on a Pkl Int outside 0..255, e.g. a byte-size or octet field set to 300 or -1.","commonSituations":"Small numeric fields (protocol octets, RGB components) configured with out-of-range values; negative sentinels.","solutions":["Validate or clamp the value to 0..255 before conversion.","Choose a wider Kotlin numeric type for the target property."],"exampleFix":"// before\nval alpha: UByte = config.to<UByte>() // value: 300\n// after\nval alpha: UInt = config.to<UInt>()","handlingStrategy":"validation","validationCode":"val v: Long = config[\"octet\"]\nrequire(v in 0..255) { \"octet must fit in UByte (0..255), got $v\" }","typeGuard":"fun Long.toUByteSafe(): UByte? = if (this in 0..255) toUByte() else null","tryCatchPattern":"val octet = try {\n  config.to<UByte>()\n} catch (e: ConversionException) {\n  0uB // or fail fast with context\n}","preventionTips":["Constrain Pkl properties: `octet: Int(isBetween(0, 255))`.","For RGB/protocol byte fields, validate components in Pkl before export.","Use a wider type when the semantic range is unclear."],"tags":["kotlin","unsigned","range","conversion"],"backgroundTag":"value-out-of-range","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"}