{"record":{"id":"18a7ace22ff1f3bb","repo":"apple/pkl","slug":"cannot-convert-pkl-base-int-value-to-kotlin-ulo","errorCode":null,"errorMessage":"Cannot convert pkl.base#Int `$value` to kotlin.ULong because it is outside range `0...${Long.MAX_VALUE}`","messagePattern":"Cannot convert pkl\\.base#Int `\\$value` to kotlin\\.ULong because it is outside range `0\\.\\.\\.(.+?)`","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/mapper/KotlinConversions.kt","lineNumber":28,"sourceCode":" * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\npackage org.pkl.config.kotlin.mapper\n\nimport java.util.*\nimport java.util.regex.Pattern\nimport org.pkl.config.java.mapper.Conversion\nimport org.pkl.config.java.mapper.ConversionException\nimport org.pkl.core.PClassInfo\n\nobject KotlinConversions {\n  val pIntToULong: Conversion<Long, ULong> =\n    Conversion.of(PClassInfo.Int, ULong::class.java) { value, _ ->\n      if (value < 0) {\n        throw ConversionException(\n          \"Cannot convert pkl.base#Int `$value` to kotlin.ULong \" +\n            // use Long.MAX_VALUE instead of ULong.MAX_VALUE\n            \"because it is outside range `0...${Long.MAX_VALUE}`\"\n        )\n      }\n      value.toULong()\n    }\n\n  val pIntToUInt: Conversion<Long, UInt> =\n    Conversion.of(PClassInfo.Int, UInt::class.java) { value, _ ->\n      val max = 0xFFFFFFFF // use literal instead of `UInt.MAX_VALUE.toLong()`\n      if (value < 0 || value > max) {\n        throw ConversionException(\n          \"Cannot convert pkl.base#Int `$value` to kotlin.UInt \" +\n            \"because it is outside range `0...$max`\"\n        )\n      }\n      value.toUInt()","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/mapper/KotlinConversions.kt#L10-L46","documentation":"Converting a Pkl Int (64-bit signed Long) to kotlin.ULong is only allowed for values >= 0, since the mapping refuses negative values even though ULong could hold them. Negative Pkl Ints cannot be represented by this conversion. Throw when value < 0.","triggerScenarios":"Calling config.to<ULong>() (or mapper conversion pIntToULong) where the Pkl Int value is negative, e.g. `offset: Int = -1`.","commonSituations":"Pkl config uses -1 as a sentinel ('unlimited', 'unset') and the Kotlin side expects an unsigned counter type.","solutions":["Fix the Pkl value to be >= 0.","Convert to Long instead of ULong if negative sentinels are legitimate.","Validate the value in Pkl (e.g. `value: Int(this >= 0)`) so misconfiguration fails at config validation time.","In Kotlin, convert to Long? and map the sentinel before requesting ULong."],"exampleFix":"// before (Pkl)\nretries: Int = -1\n// after (Pkl)\nretries: Int(this >= 0) = 3","handlingStrategy":"validation","validationCode":"val v: Long = config[\"value\"]\nrequire(v >= 0) { \"value must be >= 0 to convert to ULong, got $v\" }","typeGuard":"fun Long.toULongSafe(): ULong? = if (this >= 0) toULong() else null","tryCatchPattern":"val n = try {\n  config.to<ULong>()\n} catch (e: ConversionException) {\n  0uL // or surface a config-validation error\n}","preventionTips":["Constrain Pkl properties with `Int(this >= 0)` so bad values fail at config validation.","Avoid negative sentinel values in fields consumed as unsigned numbers.","Prefer Long/ULong over narrower unsigned types when values may grow."],"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"}