fix: Channel_RecvInt init + add channel example to README

- Initialize result to 0 in Channel_RecvInt/RecvFloat64 to handle closed channel
- Add producer/consumer channel example in examples/channel.bux
- Update README.md with Channels syntax preview
This commit is contained in:
2026-06-05 22:15:56 +03:00
parent a7f96c8e0d
commit 96aada2a4e
3 changed files with 99 additions and 2 deletions
+39
View File
@@ -124,6 +124,45 @@ func Main() -> int {
}
```
### Channels (Producer/Consumer)
```bux
import Std::Io::{PrintLine, PrintInt};
import Std::Task::{Task_Join, TaskHandle};
import Std::Channel::{Channel, Channel_New, Channel_SendInt, Channel_RecvInt, Channel_Close};
func Producer(chPtr: *Channel<int>) {
var i: int = 1;
while i <= 5 {
Channel_SendInt(chPtr, i * 10);
i = i + 1;
}
Channel_Close<int>(chPtr);
}
func Consumer(chPtr: *Channel<int>) {
var total: int = 0;
while true {
let val: int = Channel_RecvInt(chPtr);
if val == 0 { break; }
total = total + val;
PrintInt(val);
PrintLine("");
}
PrintLine("Total:");
PrintInt(total);
PrintLine("");
}
func Main() -> int {
let ch: Channel<int> = Channel_New<int>(3);
let p: *void = spawn Producer(&ch);
let c: *void = spawn Consumer(&ch);
Task_Join(TaskHandle { handle: p });
Task_Join(TaskHandle { handle: c });
return 0;
}
```
### Hash Map
```bux
import Std::Map::{Map, Map_New, Map_Set, Map_Get};