beginner question about st
#554
-
I was playing around with koka a bit and encountered something that surprised me, and it's probably due to me not understanding syntax:
why does I was expecting this to work like the following program in Haskell: i :: Int -> STRef s Int -> ST s ()
i x ref = do
modifySTRef ref \i -> x + i
j :: Int
j = runST do
r <- newSTRef 0
i 5 r
readSTRef r |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Ah yes this confused me at first too. The return value of modify is the return value of the lambda you pass it, the return value of modify is not used to set the ref. Inside the lambda |
Beta Was this translation helpful? Give feedback.
Ah yes this confused me at first too. The return value of modify is the return value of the lambda you pass it, the return value of modify is not used to set the ref. Inside the lambda
i
refers to a local variable (you can usei := i + x
to get what you want). It's closer in behavior and semantics to Haskell's modifyMVar without the need to return a tuple.