We propose SessionC#, a lightweight session typed library for safe concurrent/distributed programming. The key features are (1) the improved fluent interface which enables writing communication in chained method calls, by exploiting C#'s out variables, and (2) amalgamation of session delegation with async/await, which materialises session cancellation in a limited form, which we call session intervention. We show the effectiveness of our proposal via a Bitcoin miner application.
IntroductionSession types [8] are a theoretical framework for statically specifying and verifying communication protocols in concurrent and distributed programs. Session types guarantee that a well-typed program follows a safe communication protocol free from reception errors (unexpected messages) and deadlocks.The major gaps between session types and "mainstream" programming language type system are the absence of the two key features: (1) duality for checking the communication protocol realises reciprocal communication actions between two peers, and (2) linearity ensuring that each peer is exactly following the protocol, in the way that channel variables are exclusively used from one site for the exact number of times. Various challenges have been made for incorporating them into general-purpose languages including Java [12], Scala [28], Haskell [25,14,19,23], OCaml [24,13] and Rust [15,16].We observe that the above-mentioned gaps in session-based programming can be narrowed further by the recent advancement of programming languages, which is driven by various real-world programming issues. In particular, C# [1] is widely used in areas ranging from Windows and web application platforms to gaming (e.g. Unity), and known to be eagerly adopting various language features including async/await, reifiable generics, named parameters, out variables and extension methods.In this paper, we propose SessionC# -a library implementation of session types on top of the rich set of features in C#, and show its usefulness in concurrent/distributed programming, aiming for practicality. client server (int,int,int) «delegation» left right int choice (completed) (cancelled) cancel (asynchronously)Namely, (1) it has an improved fluent interface (i.e., method calls can be chained) via C#'s out variables, reducing the risk of linearity violation in an idiomatic way. Furthermore, (2) it enables session cancellation in a limited form -which we call session intervention -by utilising amalgamation of C#'s async/await and session delegation in thread-based concurrency. We illustrate the essential bits of SessionC# where a cancellable computation is guided by session types, by a use-case where a C# thread calculates a cancellable tak function which is designed to have a long running time [20]. The figure on the right depicts the overall communication protocol, which can be written in SessionC# as a protocol specification describing a client-server communication protocol from the client's viewpoint, as follows: var prot = Send(Val<(int,int,int)>, Deleg(chan:Recv(Unit,...