Partial Functions in Kotlin and How to Get Rid of Them

Опубликовано: 02 Май 2026
на канале: Pairing with Duncan
613
34

Partial functions are functions that can't return a result for some of their inputs,. They are a problem, because the compiler doesn't know that they can fail, and so we have to deal with failures at runtime. Today we refactor our Item constructor into a total function by using Kotlin Value Classes, simplifying a whole layer of our codebase.

This is Part 32 of an exploration of where a Test Driven Development implementation of the Gilded Rose stock control system might take us in Kotlin. You can see the whole series as a playlist
   • All Gilded Rose Episodes  
and the code on GitHub https://github.com/dmcg/gilded-rose-tdd

This sub-series on error handling is also a playlist
   • Kotlin Error Handling - Exceptions, Nulls,...  

You can read more about tiny types and partial functions on my blog http://oneeyedmen.com/failure-is-not-...

If you like this, you’ll probably like the book Java to Kotlin, A Refactoring Guidebook
http://java-to-kotlin.dev. Chapter 19 covers error handling.