Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polyphonic C# methods separated by ampersand?

I'm reading this introduction to Polyphonic C# and the first page contains this example:

Example: A Simple Buffer

Here is the simplest interesting example of a Polyphonic C# class:

public class Buffer {
    public String get() & public async put(String s) {
        return s;    
    } 
}

I don't get it at all.
What does the & between methods get() and put() signify?

like image 795
Mathias R. Jessen Avatar asked Jul 09 '13 20:07

Mathias R. Jessen


1 Answers

That's not legal C#. That's polyphonic C#. That's a research project from Microsoft. It's a shame it's not precisely defined1 but the point is this:

In Polyphonic C#, however, a body may be associated with a set of (synchronous and/or asynchronous) methods. We call such a definition a chord, and a particular method may appear in the header of several chords. The body of a chord can only execute once all the methods in its header have been called.

So & is "binding" those methods together into a chord which won't be invoked until all the methods in the chord are invoked.

A little further along in the same article:

These two methods appear (separated by an ampersand) in the header of a single chord, the body of which consists of the return statement. Now assume that b is an instance of Buffer and that producer and consumer threads wish to communicate via b. Producers make calls to put(), which, since the method is asynchronous, do not block. Consumers make calls to get(), which, since the method is synchronous, will block until or unless there is a matching call to put(). Once b has received both a put() and a get(), the body runs and the argument to the put() is returned as the result of the call to get(). Multiple calls to get() may be pending before a put() is received to reawaken one of them and multiple calls to put() may be made before their arguments are consumed by subsequent get()s.

1: But, such is the nature of cutting edge. I do get it.

like image 92
jason Avatar answered Sep 29 '22 06:09

jason