Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. const INVALID_NAME_REASON = "InvalidName";
  2.  
  3. int currentId = 1000;
  4.  
  5. // A function that attempts registering an account for a specified username;
  6. public function registerAccount(string username) returns int|error {
  7. error? res = validateUsername(username);
  8.  
  9. // Type test to check if `validateUsername(username)` returned an error.
  10. if (res is error) {
  11. // Avoid registering an account for invalid usernames.
  12. // Return the error that occurred.
  13. return res;
  14. }
  15.  
  16. // This point is reached only for a valid username.
  17. // Account addition code.
  18. currentId += 1;
  19. return currentId;
  20. }
  21.  
  22. // A username validation function that returns an `error`
  23. // on invalid usernames, or `()` if the username is valid.
  24. function validateUsername(string username) returns error? {
  25.  
  26. if (username.length() < 6) {
  27. return error(INVALID_NAME_REASON, message = "invalid length");
  28. }
  29.  
  30. if !(username.indexOf(" ") is ()) {
  31. return error(INVALID_NAME_REASON, message = "contains spaces");
  32. }
  33.  
  34. // `()` is returned if this point is reached, indicating a valid username.
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement