Skip to content

Commit

Permalink
add Result.fromUpper/toLower to facilitate interop with common IC can…
Browse files Browse the repository at this point in the history
…isters using uppercase Result variants (#626)

Interim solution for https://dx.internetcomputer.org/topic/166


---------

Co-authored-by: Claudio Russo <claudio@dfinity.org>
  • Loading branch information
ByronBecker and crusso authored Apr 16, 2024
1 parent 25d7085 commit 94ccc65
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Result.mo
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,28 @@ module {
}
};

/// Converts an upper cased `#Ok`, `#Err` result type into a lowercased `#ok`, `#err` result type.
/// On the IC, a common convention is to use `#Ok` and `#Err` as the variants of a result type,
/// but in Motoko, we use `#ok` and `#err` instead.
public func fromUpper<Ok, Err>(
result : { #Ok: Ok; #Err: Err }
) : Result<Ok, Err> {
switch result {
case (#Ok(ok)) { #ok(ok) };
case (#Err(err)) { #err(err) }
}
};

/// Converts a lower cased `#ok`, `#err` result type into an upper cased `#Ok`, `#Err` result type.
/// On the IC, a common convention is to use `#Ok` and `#Err` as the variants of a result type,
/// but in Motoko, we use `#ok` and `#err` instead.
public func toUpper<Ok, Err>(
result : Result<Ok, Err>
) : { #Ok: Ok; #Err: Err } {
switch result {
case (#ok(ok)) { #Ok(ok) };
case (#err(err)) { #Err(err) }
}
};

}

0 comments on commit 94ccc65

Please sign in to comment.