icerpc / slicec

The Slice compiler library
Apache License 2.0
13 stars 5 forks source link

Add Result Type Support #687

Closed InsertCreativityHere closed 9 months ago

InsertCreativityHere commented 9 months ago

This PR adds support for a new generic primitive type named Result. It is syntactically equivalent to a Dictionary, but uses the Result keyword instead.

unchecked enum MyError {}

typealias StringResult = Result<String, MyError>

interface I {
    getBytes() -> Result<Sequence<uint8>, MyError>
}

Result is a Slice2 only feature. It can be used anywhere a type can be. It is logically equivalent to a compact enum with 2 variants for 'ok' and 'error'.

Internally in the compiler, I named it ResultType, since Result is a heavily used type in Rust.


Deviations from the original proposal (#682)

In the original proposal, we wanted to support tuples in the Ok type:

Result<(a: bool, tag(2), b: string?), MyError>

However this implementation does not support this, since we never added support for first-class tuples to Slice. Both type parameters for Result must be Slice types.

We can always change this in the future, but note there is no lack of functionality. Users must simply convert their tuple into a struct, and then that struct can be used.

externl commented 9 months ago

An action is failing with:

single_line_let_else_max_width cannot have a value that exceeds max_width. single_line_let_else_max_width will be set to the same value as max_width