Skip to content

Commit

Permalink
Merge branch 'main' into kyay10/contract-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Nov 22, 2024
2 parents aa36f45 + 5026cfd commit c2baace
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 50 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 @@ -52,11 +52,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 @@ -169,8 +169,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 @@ -531,8 +530,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 @@ -601,8 +599,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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.validate

class OpticsProcessor(
private val codegen: CodeGenerator,
Expand All @@ -23,15 +24,16 @@ class OpticsProcessor(
) :
SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
resolver
val (resolved, deferred) = resolver
.getSymbolsWithAnnotation("arrow.optics.optics")
.filterIsInstance<KSClassDeclaration>()
.forEach(::processClass)
.partition { it.validate() }
resolved.forEach(::processClass)

// the docs say that [process] should return
// "deferred symbols that the processor can't process"
// and in theory we have none
return emptyList()
// If types used by the annotated class are by other processors,
// such class will fail the validation. In that case, we need to
// defer the code generation for the class to the next round
return deferred
}

private fun processClass(klass: KSClassDeclaration) {
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[versions]
animalSniffer = "1.7.1"
animalSniffer = "1.7.2"
arrowGradleConfig = "0.12.0-rc.24"
coroutines = "1.9.0"
classgraph = "4.8.179"
dokka = "1.9.20"
kotest = "6.0.0.M1"
kover = "0.8.3"
kotlin = "2.1.0-RC"
kotlin = "2.1.0-RC2"
kotlinBinaryCompatibilityValidator = "0.16.3"
kotlinCompileTesting = "0.6.0"
knit = "0.5.0"
kspVersion = "2.1.0-RC-1.0.27"
kspVersion = "2.1.0-RC2-1.0.28"
kotlinxSerialization = "1.7.3"
mockWebServer = "4.12.0"
retrofit = "2.11.0"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down

0 comments on commit c2baace

Please sign in to comment.