Skip to content

Commit

Permalink
Fix bug in the transaction example (#2667)
Browse files Browse the repository at this point in the history
Co-authored-by: Imran Malic Settuba <46971368+i-walker@users.noreply.github.com>
Co-authored-by: Wenkai Yuan <wenkai.yuan@crypto.com>
  • Loading branch information
3 people authored Feb 28, 2022
1 parent 1bed6da commit f7707ac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import arrow.fx.stm.internal.lookupHamtWithHash
*
* fun STM.withdraw(acc: TVar<Int>, 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
Expand Down Expand Up @@ -112,7 +112,7 @@ import arrow.fx.stm.internal.lookupHamtWithHash
*
* fun STM.withdraw(acc: TVar<Int>, 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)`
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fun STM.deposit(acc: TVar<Int>, amount: Int): Unit {

fun STM.withdraw(acc: TVar<Int>, 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!")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fun STM.deposit(acc: TVar<Int>, amount: Int): Unit {

fun STM.withdraw(acc: TVar<Int>, 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)`
}
Expand Down

0 comments on commit f7707ac

Please sign in to comment.