edge_types.mx raw

   1  // Phase 0c, first slice: the declared channel ownership types exist as BUILTIN
   2  // generics in the universe, on both compilers.
   3  //
   4  //   Sender[T]   = chan<- T   (one writer)
   5  //   Receiver[T] = <-chan T   (one reader)
   6  //   Edge[In, Out] = { Recv Receiver[In]; Send Sender[Out] }
   7  //
   8  // The declaration form (`edge[In, Out]{depth}`) and spawn agreement are the
   9  // next slice; this one pins that the types are predeclared (no import), that a
  10  // channel converts to either directional end, that an instantiated Edge field
  11  // carries the instantiation (a send through e.Send arrives at e.Recv), and that
  12  // the operation-level role check applies through an Edge field on both
  13  // compilers.
  14  //
  15  //:wasmtest skip
  16  package main
  17  
  18  import "fmt"
  19  
  20  func main() {
  21  	select {
  22  	case <-shutdown:
  23  		return
  24  	default:
  25  	}
  26  	ch := chan int32{:1}
  27  	var e Edge[int32, int32]
  28  	e.Send = Sender[int32](ch)
  29  	e.Recv = Receiver[int32](ch)
  30  	e.Send <- 42
  31  	if (<-e.Recv) != 42 {
  32  		panic("Edge field lost its instantiated element type")
  33  	}
  34  
  35  	// A one-way handoff: the writer end alone, then the reader end alone.
  36  	out := chan int64{:1}
  37  	var w Sender[int64] = Sender[int64](out)
  38  	w <- 7
  39  	var r Receiver[int64] = Receiver[int64](out)
  40  	if (<-r) != 7 {
  41  		panic("directional end lost its element")
  42  	}
  43  	fmt.Println("edge types ok")
  44  }
  45