Result
Result is a built-in generic type that either holds a Success value or a Failure value, where Success and Failure are type parameters.
You can construct a result inline, without giving it a name, usually to specify the return type of an operation. For example:
module VisitorCenter
interface Greeter { greet(name: string) -> Result<string, GreeterError>}
/// The errors returned by the Greeter.unchecked enum GreeterError { EmptyName NameTooLong(maxLength: int32) Away(until: WellKnownTypes::TimeStamp)}You can also use a constructed result as the type of a field, the element type of a sequence, the type of an operation parameter, and so on, just like any other Slice type.
A result is conceptually equivalent to a compact variant enum with two variants (Success and Failure), each with a single field of the associated type. You can see Result<string, GreeterError> as a convenient way to reference:
compact enum Result<string, GreeterError> { // not a valid Slice identifier Success(value: string) Failure(value: GreeterError)}C# mapping
Result<Success, Failure> maps to Result<TSuccess, TFailure>, where Result<TSuccess, TFailure> is a C# record class in the ZeroC.Slice namespace.
The Slice type parameters are mapped to C# type parameters, following the usual mapping rules. For example:
| Slice | C# mapping |
|---|---|
Result<string, int32> | Result<string, int> |
Result<WellknownTypes::TimeStamp, int32> | Result<DateTime, int> |
Result<string?, varint32> | Result<string?, int> |
Result<MyStruct, MyEnum> | Result<MyStruct, MyEnum> |
The C# Result record class is a discriminated union with a number of helper methods provided by Dunet.
For example, you can process the Result<string, GreeterError> shown earlier with the following code:
// Match and MatchAway are methods generated Dunet.string message = result.Match( success => success.Value, failure => failure.Value.MatchAway( away => $"Away until {away.Until.ToLocalTime()}", () => $"{failure.Value}"));