Functions

The following functions are available globally.

  • Send a message over a channel. Sending over a closed channel will raise a runtime exception.

    Declaration

    Swift

    public func <-<T>(l: Chan<T>, r: T)
  • Receive a message over a channel. Returns nil if the channel is closed

    Declaration

    Swift

    public prefix func <-<T>(r: Chan<T>) -> T?
  • A dispatch statement starts the execution of an action as an independent concurrent thread of control within the same address space.

    Declaration

    Swift

    public func dispatch(action: ()->())
  • A select statement chooses which of a set of possible send or receive operations will proceed. It looks similar to a switch statement but with the cases all referring to communication operations.

    var c1 Chan<String>()
    var c2 Chan<String>()
    
    // Each channel will receive a value after some amount of time, to simulate e.g. blocking RPC operations executing in concurrent operations.
    dispatch {
        sleep(1)
        c1 <- "one"
    }
    dispatch {
        sleep(2)
        c2 <- "two"
    }
    
    // We’ll use select to await both of these values simultaneously, printing each one as it arrives.
    for var i = 0; i < 2; i++ {
        _select {
            _case (msg1) { c1 in
                print("received", msg1)
            }
            _case (msg2) { c2 in
                print("received", msg2)
            }
        }
    }
    

    Declaration

    Swift

    public func _select(block: ()->())
  • A case statement reads messages from a channel.

    Declaration

    Swift

    public func _case<T>(chan: Chan<T>, block: (T?)->())
  • A default statement will run if the case channels are not ready.

    Declaration

    Swift

    public func _default(block: ()->())