Advertisement
tinyevil

Untitled

May 9th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.19 KB | None | 0 0
  1. -- enum
  2. type Day with
  3.     case Saturday
  4.     case Sunday
  5.     case Meh
  6. end
  7.  
  8. -- struct
  9. type User with
  10.     field uid:i64
  11.     field name:string
  12.     field language:LangCode
  13. end
  14.  
  15. -- generic struct
  16. type Point2D[T] with
  17.     field x y:T
  18. end
  19.  
  20. -- sum type
  21. type HttpResponse with
  22.     case Timeout
  23.     case OK(body:string)
  24.     case NotFound
  25.     case Redirect(url:string)
  26. end
  27.  
  28. -- nested type
  29. type HttpResponse with
  30.     case Response(code:i32) with
  31.         case Success with
  32.             case OK
  33.             case Created
  34.             case Accepted
  35.         end
  36.        
  37.         case Redirection(url:string) with
  38.             case Permanent
  39.             case Temporary
  40.         end
  41.        
  42.         case ClientError
  43.             case BadRequest
  44.             case NotFound
  45.             case Forbidden
  46.         end
  47.        
  48.         case ServerError
  49.             case Internal
  50.             case BadGateway
  51.         end
  52.        
  53.         field headers:List[{string,string}]
  54.         field body:string
  55.     end
  56.    
  57.     case NetworkError(os_code:string)
  58.     case Timeout
  59. end
  60.  
  61. -- working with HttpResponse
  62. function foo(r:HttpResponse)
  63.     match r with
  64.     case Success as r then
  65.         print(r.body)
  66.     case Redirection(url) then
  67.         foo(make_request(url))
  68.     case Response(code) then
  69.         print("Bad response")
  70.     case Timeout then
  71.         print("Timeout")
  72.     case NetworkError(err) then
  73.         print("Network error: ", err)
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement