0:00 / 0:00
#kotlin#contextreceivers#functionalprogramming
Defining an IO Context in Kotlin
Pairing with Duncan
4.5K subscribers • 15 videos on Hallway
- Recommended by 1 person: 1 in engineering communities.
- Linked in the descriptiondmcg/gilded-rose-tdd
- May be out of date. The tools, versions or practices it shows have likely moved on.
Intermediate. Assumes working knowledge.
1.2K views3 years ago1 recommendation
Programming languagesSoftware designKotlinFunctional programming
Duncan McGregor explores using Kotlin context receivers to mark impure IO actions and enforce functional programming boundaries at compile time.
In the last episode (youtu.be/UOY0aSpwksE) we learned to distinguish actions from calculations, or, as functional programmers would have it, impure from pure functions. We saw that calculations are easier to test, refactor and reason with, but actions tend to proliferate, because code that calls an action becomes an action itself. We would like to minimize the proportion of actions, so we used annotations to mark them to warn us when they appear. The problem with annotations is that the compiler doesn't really understand them, so it can't tell if we have missed an annotation. We tried here (youtu.be/eXKKrYc6Myg) but couldn't find any way to make it work. The compiler does (mostly) understand the new context receivers though, so in this episode we try using contexts to mark actions, starting with the most common reason for code being impure - IO. Thanks to Jordan Stewart (twitter.com/damaged) for collaborating on this, part 66 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 If you like this, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (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. I have some free time between producing videos and working for team Gilded Rose. If you like these videos I'd like to work with you - please get in touch - duncan@oneeyedmen.comWhy it's here
1 person recommends thisWhat people say. Recommended as a practical exploration of using Kotlin context receivers for functional programming.
u/dmcgin r/Kotlin • Mentions
On “Cunning Kotlin Context Receivers”
From the description :-
In the last episode, Defining an IO Context in Kotlin ([video we used context receivers to taint code that uses IO so that we can track it through our code. Why? See Master Functional Thinking -Actions, Calculations, Data ([video
One of the loose ends from that episode was that we were left with more complicated tests, so we start by writing a JUnit extension to allow itto invoke tests with a context. Then I find a way to reuse our retry higher-order function with contexts, only to find that Youssef Shoaib (https://github.com/kyay10)already had a far cunninger plan, so we then find a way to refactor tothat solution.
This is part 67 of an exploration of where a…
u/kyay10in r/Kotlin • Mentions
On “Context Receivers - Does It Support "OR" Scenarios?”
Very simple. Since you're not using the context itself to provide a value but merely as a syntactic "key", just define your function as `private` in the file, and then provide 3 public overloads each with a different "key" concept type. I do like the approach of using contexts as keys since it allows for easier code coloring. If you're interested btw, check out this video by Duncan McGregor on using context receivers to color code that does IO.
Btw, asking on the Kotlin slack might get you better answers since a lot more of us there are using context receivers (and loving it!)