Skip to content
0:00 / 0:00

#kotlin#valueclasses#tinytypes

Kotlin Value Classes - Tiny Types to Eschew Errors

Pairing with Duncan

4.5K subscribers • 15 videos on Hallway

YouTube
  • Recommended by 1 person: 1 in engineering communities.
  • Presented by Duncan McGregor
  • Linked in the descriptiondmcg/gilded-rose-tdd

Intermediate. Assumes working knowledge.

1.4K views4 years ago1 recommendation

Programming languagesSoftware designKotlinDesign patternsRefactoring and legacy code

Duncan McGregor demonstrates how to use Kotlin value classes to create expressive, type-safe tiny types and avoid primitive obsession.

We can usually avoid the errors that result from passing inappropriate arguments to functions, or at least move them from inside the function to outside, where we probably have more context. Kotlin Value Classes (previously known as Inline Classes) are good for this, allowing us to create Tiny Types that are more restricted and expressive than primitives. This is Part 30 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 youtube.com/playlist?list=PL1ssMPpyqociJNwykAOB9_KEZVW7BW7m2 and the code on GitHub github.com/dmcg/gilded-rose-tdd This sub-series on error handling is also a playlist youtube.com/playlist?list=PL1ssMPpyqochiZj41oLAtvht4ScUurHJH You can read more about tiny types and partial functions on my blog oneeyedmen.com/failure-is-not-an-option-part-7.html As far as I can see, Darren Hobbs coined the term Tiny Types darrenhobbs.com/2007/04/11/tiny-types/ If you like this, you’ll probably like the book Java to Kotlin, A Refactoring Guidebook java-to-kotlin.dev. Chapter 19 covers error handling.

Why it's here

1 person recommends this

What people say. Recommended as a practical demonstration of using Kotlin value classes to improve type safety.

u/dmcgin r/Kotlin • Recommends

Kotlin Value Classes - Tiny Types to Eschew Errors [video And for those of you who like the plain-old-blog-posts http://oneeyedmen.com/failure-is-not-an-option-part-7.html

u/dmcgin r/Kotlin • Mentions

On “🤢 Life is too short for working with blank strings.”

As it happens I made a video about exactly this topic [video The next few in the series [video [video link to this video also look at tiny tyes

u/dmcgin r/Kotlin • Mentions

On “why does invoke operator exist?”

The key here is writing something that looks like a ctor but returns not the type from the outer class. So we can return List<T> from a call to ConcreteList<T>(…), or say NonZeroInt? from a call to NonZeroInt(-4). You can see the point of the later here Tiny Types to Eschew Errors link to this video