From f7707ac2301a7c3b4fb1819ba9b6826f388a2cd8 Mon Sep 17 00:00:00 2001 From: SaberCon <894876785@qq.com> Date: Mon, 28 Feb 2022 18:13:03 +0800 Subject: [PATCH] Fix bug in the transaction example (#2667) Co-authored-by: Imran Malic Settuba <46971368+i-walker@users.noreply.github.com> Co-authored-by: Wenkai Yuan --- .../fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt | 4 ++-- .../src/jvmTest/kotlin/examples/example-stm-01.kt | 2 +- .../src/jvmTest/kotlin/examples/example-stm-02.kt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt b/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt index aa5ca900e0c..895be245622 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/commonMain/kotlin/arrow/fx/stm/STM.kt @@ -55,7 +55,7 @@ import arrow.fx.stm.internal.lookupHamtWithHash * * fun STM.withdraw(acc: TVar, amount: Int): Unit { * val current = acc.read() - * if (current - amount >= 0) acc.write(current + amount) + * if (current - amount >= 0) acc.write(current - amount) * else throw IllegalStateException("Not enough money in the account!") * } * //sampleEnd @@ -112,7 +112,7 @@ import arrow.fx.stm.internal.lookupHamtWithHash * * fun STM.withdraw(acc: TVar, amount: Int): Unit { * val current = acc.read() - * if (current - amount >= 0) acc.write(current + amount) + * if (current - amount >= 0) acc.write(current - amount) * else retry() // we now retry if there is not enough money in the account * // this can also be achieved by using `check(current - amount >= 0); acc.write(it + amount)` * } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt index c74156a49ce..c48332f9f87 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-01.kt @@ -18,7 +18,7 @@ fun STM.deposit(acc: TVar, amount: Int): Unit { fun STM.withdraw(acc: TVar, amount: Int): Unit { val current = acc.read() - if (current - amount >= 0) acc.write(current + amount) + if (current - amount >= 0) acc.write(current - amount) else throw IllegalStateException("Not enough money in the account!") } diff --git a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt index bdeaf829b0b..af8152800bb 100644 --- a/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt +++ b/arrow-libs/fx/arrow-fx-stm/src/jvmTest/kotlin/examples/example-stm-02.kt @@ -21,7 +21,7 @@ fun STM.deposit(acc: TVar, amount: Int): Unit { fun STM.withdraw(acc: TVar, amount: Int): Unit { val current = acc.read() - if (current - amount >= 0) acc.write(current + amount) + if (current - amount >= 0) acc.write(current - amount) else retry() // we now retry if there is not enough money in the account // this can also be achieved by using `check(current - amount >= 0); acc.write(it + amount)` }