Skip to content

Commit

Permalink
Update the example
Browse files Browse the repository at this point in the history
  • Loading branch information
Shon Feder committed Aug 5, 2023
1 parent 02b7bbb commit b563176
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions doc/rfcs/rfc001-sum-types/rfc001-erc-sum-types-example.qnt
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,28 @@ module erc20 {

// Example of a sum type representing a
// NOTE: Once we support parametric sum-types we can generalize this alias and the map operators
type Erc20Result = {
Ok(Erc20State),
Error(str),
}

pure def mapState(r:Erc20Result, f:Erc20State => Erc20State): Erc20Result = r match {
Ok(s) => Ok(f(s)),
Error(_) => r
}

pure def flatMap(r:Erc20Result, f:Erc20State => Erc20Result): Erc20Result = r match {
Ok(s) => f(s),
Error(_) => r
}
type Erc20Result =
| Ok(Erc20State)
| Error(str)

pure def mapState(r:Erc20Result, f:Erc20State => Erc20State): Erc20Result =
match r {
| Ok(s) => Ok(f(s))
| Error(_) => r
}

pure def flatMap(r:Erc20Result, f:Erc20State => Erc20Result): Erc20Result =
match r {
| Ok(s) => f(s)
| Error(_) => r
}

// An auxilliary definition similar to Solidity's require
pure def require(r: Erc20Result, cond: bool, msg: str): Erc20State = match r {
Error(_) => s,
Ok(s) => if (cond) Ok(s) else Error(msg)
}
pure def require(r: Erc20Result, cond: bool, msg: str): Erc20Result =
match r {
| Error(_) => r
| _ => if (cond) r else Error(msg)
}

// contract initialization
pure def newErc20(sender: Address, initialSupply: Uint): Erc20State = {
Expand Down

0 comments on commit b563176

Please sign in to comment.