Supercharge your Kotlin with Java Dynamic Proxies

Published: 05 July 2024
on channel: Pairing with Duncan
916
56

We left the last episode (   • Refactoring to the Repository Pattern  ) with the realisation that we can’t automagically delegate from an interface of suspend functions to an interface of non-suspend functions.

That seems like quite an arcane problem, but the solution is generally very useful. It turns that we can implement interfaces at runtime, running dynamic code to respond to method calls. This can save us from writing lots of boilerplate code, and is used in dependency injection, object relational mapping and mocking.

Unfortunately this technique is only applicable on the JVM, so if you are a Kotlin purist you may wish to look away now. For the rest of us, let’s have fun with dynamic proxies

In this episode

00:00:42 A tale of two interfaces
00:02:34 Implementing only one method in an interface with an object
00:03:31 Implementing an interface with a dynamic proxy
00:07:16 Dynamic proxies and Kotlin delegation are a match made in heaven
00:08:33 Generalise generalize generalise
00:10:31 IntelliJ bug moving a function to a new package?
00:11:40 IntelliJ bug moving file
00:12:05 Proxies for structural typing
00:20:59 Just one wafer-thin fly in the ointment

The code for this video is on a GitHub fork https://github.com/dmcg/ktor-document...

This video is in a playlist of Ktor episodes (   • Ktor  ) and http4k (   • http4k  )

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.


Watch video Supercharge your Kotlin with Java Dynamic Proxies online without registration, duration hours minute second in high quality. This video was added by user Pairing with Duncan 05 July 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 916 once and liked it 56 people.