Skip to content

Commit

Permalink
Update examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
alpacaaa committed Sep 11, 2023
1 parent cd648c7 commit 1d8d008
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
5 changes: 5 additions & 0 deletions compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,11 @@ if {is_matching} != 2 {{
inner.emit(format!("case {ident} := {new_expr}:",));
}

Pat::Wild { .. } => {
let new_expr = self.emit_select_case(&expr, &mut out);
inner.emit(format!("case {new_expr}:",));
}

_ => unreachable!(),
},

Expand Down
58 changes: 44 additions & 14 deletions playground/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,11 +827,25 @@ fn main() {

## Select statements

There's no first class support for `select {}` statements yet. still use them
with a bit of a hacky workaround.
`select {}` works like in Go, however the syntax is slightly different.

You can get around this by using `rawgo` which lets you embed raw Go code.
First-class support for `select` statements will be available soon.
```
Reading from a channel

Go: case x := <- ch
Borgo: let x = ch.Recv()


Sending to a channel

Go: case ch <- x
Borgo: ch.Send(x)

Default case

Go: default
Borgo: _
```

```rust
use fmt
Expand All @@ -841,22 +855,38 @@ fn main() {
let (tx1, rx1) = Channel.new()
let (tx2, rx2) = Channel.new()

// dummy done channel
let (_, done) = Channel.new()

spawn (|| {
tx1.Send("a")
})()

spawn (|| {
time.Sleep(2 * time.Second)
tx2.Send("b")
loop {
select {
// in Go:
// case tx2 <- "b":
tx2.Send("b") => {
fmt.Println("sending b")
time.Sleep(1 * time.Second)
}

let _ = done.Recv() => return
}
}
})()

@rawgo (
\\ select {
\\ case a := <-rx1:
\\ fmt.Println("got", a)
\\ case b := <-rx2:
\\ fmt.Println("got", b)
\\ }
)
select {
// in Go:
// case a := <- rx1:
let a = rx1.Recv() => {
fmt.Println("got", a)
},

let b = rx2.Recv() => {
fmt.Println("got", b)
},
}
}
```

0 comments on commit 1d8d008

Please sign in to comment.