Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const INVALID_NAME_REASON = "InvalidName";
- int currentId = 1000;
- // A function that attempts registering an account for a specified username;
- public function registerAccount(string username) returns int|error {
- error? res = validateUsername(username);
- // Type test to check if `validateUsername(username)` returned an error.
- if (res is error) {
- // Avoid registering an account for invalid usernames.
- // Return the error that occurred.
- return res;
- }
- // This point is reached only for a valid username.
- // Account addition code.
- currentId += 1;
- return currentId;
- }
- // A username validation function that returns an `error`
- // on invalid usernames, or `()` if the username is valid.
- function validateUsername(string username) returns error? {
- if (username.length() < 6) {
- return error(INVALID_NAME_REASON, message = "invalid length");
- }
- if !(username.indexOf(" ") is ()) {
- return error(INVALID_NAME_REASON, message = "contains spaces");
- }
- // `()` is returned if this point is reached, indicating a valid username.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement