Skip to content

Commit

Permalink
Remove context receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Nov 21, 2024
1 parent 628addd commit 1a0dcd7
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 41 deletions.
5 changes: 0 additions & 5 deletions arrow-libs/core/arrow-autoclose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,3 @@ kotlin {
(project.rootProject.properties["kotlin_api_version"] as? String)?.also { apiVersion = KotlinVersion.fromVersion(it) }
}
}

// enables context receivers for Jvm Tests
tasks.named<KotlinCompile>("compileTestKotlinJvm") {
compilerOptions.freeCompilerArgs.add("-Xcontext-receivers")
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ import kotlin.coroutines.cancellation.CancellationException
* So both [AutoCloseScope], and `ResourceScope` behave correctly when encountering cancellation, by closing the source,
* but `ResourceScope` allows inspecting _complete_, _failure_, **and** _cancellation_ in the finalizer.
*
* This DSL works very well with Kotlin's experimental feature context receivers, soon called context parameters.
* We can write the same code from above as a function:
* We can write the same code from above as a function by adding the scope as receiver:
*
* ```kotlin
* context(AutoCloseScope)
* fun copyFiles(input: String, output: String) {
* fun AutoCloseScope.copyFiles(input: String, output: String) {
* val scanner = install(Scanner(input))
* val printer = install(Printer(output))
* for(line in scanner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class Printer(private val path: String) : AutoCloseable {
override fun close(): Unit = Unit
}

context(AutoCloseScope)
fun copyFiles(input: String, output: String) {
fun AutoCloseScope.copyFiles(input: String, output: String) {
val scanner = install(Scanner(input))
val printer = install(Printer(output))
for(line in scanner) {
Expand Down
5 changes: 0 additions & 5 deletions arrow-libs/core/arrow-cache4k/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,3 @@ tasks.named<Jar>("jvmJar").configure {
attributes["Automatic-Module-Name"] = "arrow.cache4k"
}
}

// enables context receivers for Jvm Tests
tasks.named<KotlinCompile>("compileTestKotlinJvm") {
compilerOptions.freeCompilerArgs.add("-Xcontext-receivers")
}
5 changes: 0 additions & 5 deletions arrow-libs/core/arrow-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ kotlin {
}
}

// enables context receivers for Jvm Tests
tasks.named<KotlinCompile>("compileTestKotlinJvm") {
compilerOptions.freeCompilerArgs.add("-Xcontext-receivers")
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ import kotlin.jvm.JvmName
* ```
* <!--- KNIT example-raise-03.kt -->
*
* Adding your own syntax to `Raise<R>` is not advised, yet, but will be easy once "Multiple Receivers" become available.
* Adding your own syntax to `Raise<R>` is not advised, yet, but will be easy once context parameters become available.
*
* ```
* context(Raise<R>)
* context(_: Raise<R>)
* suspend fun <R, A> Either<R, A>.bind(): A =
* when (this) {
* is Either.Left -> raise(value)
* is Either.Right -> value
* }
*
* context(Raise<None>)
* context(_: Raise<None>)
* fun <A> Option<A>.bind(): A =
* fold({ raise(it) }, ::identity)
* ```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ public interface Raise<in Error> {
*
* data class Config(val mode: Int, val role: String, val serviceType: ServiceType)
*
* context(Raise<String>)
* fun readConfig(): Config {
* fun Raise<String>.readConfig(): Config {
* val mode = ensureNotNull(readln().toIntOrNull()) {
* "Mode should be a valid integer"
* }
Expand Down Expand Up @@ -530,8 +529,7 @@ public inline fun <reified T : Throwable, A> catch(block: () -> A, catch: (t: T)
* object ContainsInvalidChars : CountryCodeError
* }
*
* context(Raise<CountryCodeError>)
* fun countryCode(rawCode: String): CountryCode {
* fun Raise<CountryCodeError>.countryCode(rawCode: String): CountryCode {
* ensure(rawCode.length == 2) { CountryCodeError.InvalidLength(rawCode.length) }
* ensure(rawCode.any { !it.isLetter() }) { CountryCodeError.ContainsInvalidChars }
* return CountryCode(rawCode)
Expand Down Expand Up @@ -600,8 +598,7 @@ public inline fun <Error> Raise<Error>.ensure(condition: Boolean, raise: () -> E
* object NullValue : NameError
* }
*
* context(Raise<NameError>)
* fun fullName(name: String?): FullName {
* fun Raise<NameError>.fullName(name: String?): FullName {
* val nonNullName = ensureNotNull(name) { NameError.NullValue }
* return FullName(nonNullName)
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ sealed interface OtherError : MyError {
object Actual : OtherError
}

context(Raise<SubError>)
suspend fun subprogram(): Unit =
suspend fun Raise<SubError>.subprogram(): Unit =
println("Hello SubProgram!")

context(Raise<OtherError>)
suspend fun otherprogram(): Unit =
suspend fun Raise<OtherError>.otherprogram(): Unit =
println("Hello OtherProgram!")

context(Raise<OtherError>)
suspend fun fail(): MyResponse =
suspend fun Raise<OtherError>.fail(): MyResponse =
raise(OtherError.Actual)

fun main() =
Expand All @@ -38,12 +35,10 @@ object EmptyResponse : MyResponse
data class ErrorResponse(val error: Throwable) : MyResponse
data class BodyResponse(val body: String) : MyResponse

context(Raise<SubError>)
suspend fun respondWithBody(): BodyResponse =
suspend fun Raise<SubError>.respondWithBody(): BodyResponse =
BodyResponse("Hello Program!")

context(Raise<OtherError>)
suspend fun attemptOrError(): MyResponse =
suspend fun Raise<OtherError>.attemptOrError(): MyResponse =
ErrorResponse(RuntimeException("Oh no!"))

fun respond(): Effect<MyError, MyResponse> =
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ retrofit = "2.11.0"
moshi = "1.15.1"
spotlessVersion = "6.25.0"
compose = "1.7.5"
composePlugin = "1.8.0+check"
composePlugin = "1.7.1"
agp = "8.7.2"
android-compileSdk = "34"
cache4k = "0.13.0"
Expand Down

0 comments on commit 1a0dcd7

Please sign in to comment.