/counter.js Secret
Last active
February 2, 2025 18:01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's say we have a block of code (boc) called `counter` with a variable `value` | |
counter : { | |
value: 0 | |
// `inc` is an internal boc, and has access to outer scope | |
inc: { value = value + 1 } | |
} | |
// Two other bocs `foo` and `bar` could read `counter.value` but can't write to it. | |
// Only the original boc `counter` can (or inner bocs) | |
foo: { | |
counter.value // do something with value | |
// counter.value = 1 would be a compilation error | |
counter.inc() | |
} | |
bar: { | |
counter.value | |
counter.inc() | |
} | |
//When `foo` and `bar` execute, the'll run concurrently and they will call counter.inc() | |
// The boc `counter` will be the only writer and will execute `inc` sequentially behaving like | |
// (think of Actors / Redis ) | |
main: { | |
foo() | |
bar() | |
// foo and bar execute concurrently, think of Go's `go foo(); go bar()` | |
// and synchronize at this point | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment