我正在阅读 this introduction to Polyphonic C#第一页包含此示例:
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; } }
我完全不明白。
方法 get()
和 put()
之间的 &
表示什么?
最佳答案
这不是合法的 C#。那是和弦 C#。那是一个 research project from Microsoft .遗憾的是它没有精确定义1 但重点是:
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.
因此,&
将这些方法“绑定(bind)”到一个 chord 中,直到调用 chord 中的所有方法时才会调用该 chord。
再往前走一点 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 ofBuffer
and that producer and consumer threads wish to communicate viab
. Producers make calls toput()
, which, since the method is asynchronous, do not block. Consumers make calls toget()
, which, since the method is synchronous, will block until or unless there is a matching call to put(). Onceb
has received both aput()
and aget()
, the body runs and the argument to theput()
is returned as the result of the call toget()
. Multiple calls toget()
may be pending before aput()
is received to reawaken one of them and multiple calls toput()
may be made before their arguments are consumed by subsequentget()
s.
1:但是,这就是前沿的本质。我明白了。
关于c# - 用&符号分隔的和弦C#方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557459/