Abolishing Kotlin Context Receivers

Опубликовано: 04 Октябрь 2024
на канале: Pairing with Duncan
963
31

They say that no good deed goes unpunished, and it is a curse to live in interesting times. Welcome to life as a professional software developer.

18 months ago, I thought that Context Receivers were going to be a useful addition to the Kotlin language. So I published a series of episodes where I used them in the Gilded Rose stock control system, firstly just as a marker to show where code performs IO, and then as a way to pass around an active database transaction.

Since then, work on context receivers has stalled. To be fair they always were experimental, but the result of that experiment is that they have been killed off in favour of context parameters, which are more explicit and less elegant.

Even more annoying is that there is no migration path. In fact, context receivers will be removed from the compiler before context parameters are introduced. So if our codebase is not to skip at least one compiler version we are going to have to refactor to replace all our uses of context receivers. And then wait. And then refactor to use context parameters where we had been using context receivers.

Sigh

In this episode

00:01:31 IntelliJ crash out of the gate
00:01:52 Looking at where we are using Context Receivers
00:03:23 Removing Context IO
00:03:58 Plan a refactoring - Bottom up
00:06:13 Interfaces cause problems because we have to fix up implementations at the same time
00:07:01 Check in to bank changes before a risky refactor
00:07:38 No automated refactoring support
00:07:52 so we will just lean on the compiler
00:09:26 Context receivers also have issues with dependency tracking
00:10:46 Ding ding - Round 2
00:12:25 Unused contexts are a bit strange
00:14:03 Continue fixing compile errors until something works
00:14:45 Now dig up
00:16:26 We can tidy some tests
00:17:29 Rollback and replay when we run into trouble
00:19:56 Now we have a rhythm
00:22:25 Rinse and repeat
00:24:01 Now we have just the top level
00:25:52 IntelliJ Inline bug
00:27:02 Review the entire commit
00:28:19 What have we learned?

There is a playlist of TDD Gilded Rose episodes -    • Test Driven Gilded Rose in Kotlin  

The codebase is available on GitHub https://github.com/dmcg/gilded-rose-tdd

I get lots of questions about the test progress bar. It was written by the inimitable @dmitrykandalov. To use it install his Liveplugin (https://plugins.jetbrains.com/plugin/...) and then this gist https://gist.github.com/dmcg/1f56ac39...

Thanks to Orion Williams for the music https://toolofgod.com/my-music/royalt...

If you like this video, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style.


Смотрите видео Abolishing Kotlin Context Receivers онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Pairing with Duncan 04 Октябрь 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 963 раз и оно понравилось 31 людям.