4 ms·
I don’t believe that strict “session types” are a good fit for Rust: it’s shoehorning a concept, simultaneously specialised (it’s for channels) and generalised
by chrismorgan 8y ago
I don’t believe that strict “session types” are a good fit for Rust: it’s shoehorning a concept, simultaneously specialised (it’s for channels) and generalised (as in “left” and “right”), into Rust in a way that works, but is not as pleasant as can be achieved with little effort. (There’s not much actual code in session-types/src/lib.rs—most of it is interfaces and macros because it made life hard for itself by being too generic.)
The underlying concept in Rust is basically a type-level enum (state machine), encoding the variant (state) as a phantom type in a generic parameter; that, you can express without the full baggage of what is called session types, and with greater freedom.
The situation is similar to the Either enum with its Left and Right variants: it was in the standard library well before Rust 1.0 because it was easy, but it was eventually abandoned in favour of good semantics: most users should use Result, and the rest should write their own two-variant enums. Similarly, if you encode your state machine in Rust types yourself, you end up with clearer and more obvious code.
The main thing this library does is get you the specific channel behaviour; that may be useful in some situations, but you can implement the underlying concept atop regular channels without much difficulty, and it gives you considerably more freedom in the API.
Here’s a small macro library that I wrote a few years ago, with an example that demonstrates this general concept: https://github.com/chris-morgan/phantom-enum https://github.com/chris-morgan/phantom-enum
I find myself wishing more and more often that Rust enum variants could be used as types, and that type refinement could work like in TypeScript.
(I do realise that what I’m advocating is targeting a potentially different and broader sector to session-types, but I maintain my position: doing it from scratch yourself is fairly straightforward, normally easy and more powerful.)
You can achieve all of this with different types instead of one type with a phantom enum type parameter, too; see the insanitybit article linked elsewhere in these comments. The primary benefit of the phantom enum type parameter approach is when you have methods that apply in multiple, or all, states, so that you don’t need to duplicate them. Sometimes you want extra data in particular states; you can achieve that in the type parameter approach by making the enum not phantom, and rather a struct with extra data, or you can achieve that by just using distinct types. Which is most appropriate varies case by case.
- bsaul 8y agoDo you know of any programming language where this type of things (having functions defined only for certain enum values, or anything equivalent) would be doable properly, without hacking the typesystem ?
- chrismorgan 8y agoLook for languages with dependent typing. Idris and Liquid Haskell are the two that spring to my mind. Other than that, TypeScript has decent support for the concept, though with standalone functions rather than methods: you’d define your “variants” as types (or interfaces), define the “enum” as the union of those types (e.g. `Foo | Bar`), and then have your functions take either the entire “enum” type, or specific “variant” types. In place of Rust’s `match`, you will then use type guards to narrow the “enum” type down to one (or more, if you like) of the “variant” types. See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types https://www.typescriptlang.org/docs/handbook/advanced-types.... for a bit of info.
- namibj 8y agoJula allows it, initially only defining the integer division operator for non-zero divisor. That default changed due to a (at that point) lack of syntactic sugar and type-inference to not make it a massive hassle. Edit: It's called a partial function, and I confused Julia with Pony. The former might be able to express it, the latter can handle it in some way, but I'm not familiar with either type system.