SHARE
TWEET

Untitled

tinyevil Dec 20th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // typedef names a type - consider it a forward declaration of a type name
  2. // of unknown kind
  3. // typedef always makes a distinct type - it is not an alias
  4. typedef exit_code;
  5.  
  6. // one way to define a type is by making it a wrapper of another type
  7. // it is still a distinct type - no implicit conversion is allowed, no subtyping
  8. // relation is established, new type's namespace has no access to the privates
  9. // of the original type and vice versa.
  10. typedef exit_code(u32);
  11.  
  12. // this type still has its own namespace, so we can put stuff in there
  13. // the keyword is WIP
  14. implementation exit_code{
  15.     function is_success(code: exit_code): bool{
  16.         return code.value == 0;
  17.     }
  18. }
  19.  
  20. // another way to define a type is struct definition
  21. // note that you cannot put any functions inside the struct - it is strictly
  22. // for data fields
  23. partial struct Rect{
  24.     var x y: f32;
  25.    
  26.     // this lets you forward declare part of the struct
  27.     // but the full definition must agree with all the partial definitions
  28.     // in particular, the defintion of this form requires the full definiton
  29.     // to have two floating point variables named x and y to be the first
  30.     // two fields in the definition
  31.     ...
  32. }
  33.  
  34. struct Rect{
  35.     var x y: f32;
  36.     var width height: f32;
  37.     // now the definition is complete
  38. }
  39.  
  40.  
  41. implementation Rect{
  42.     // you may give definitions or declarations
  43.     // ref is lifetime-checked non-owning non-nullable pointer
  44.     function area(self: ref Rect): f32;
  45.    
  46.     function perimeter(self: ref Rect): f32{
  47.         // you are allowed to use '.' on the `ref T` because it implements
  48.         // the proxy[T] protocol - when the name lookup fails on the `ref T`
  49.         // itself - and it does as `ref T` does not have any dependent names -
  50.         // the search continues on the underlying type `T` (and the protocol
  51.         // implementation describes how exactly to obtain a value of type `T`
  52.         // from the type `ref T`)
  53.         return (self.width + self.height) * 2;
  54.     }
  55. }
  56.  
  57. // you can provide definitions outside the implementation scope if a declartion
  58. // is given in the scope
  59. function Rect.area(self: ref Rect): f32{
  60.     return self.width * self.height;
  61. }
  62.  
  63. // you may open a namespace multiple times (they act exactly as if they were
  64. // one definition)
  65. implementation Rect{
  66.     function expand(self: ref Rect, dx dy: f32){
  67.         self.x -= dx;
  68.         self.y -= dy;
  69.         self.width += dx * 2;
  70.         self.height += dy * 2;
  71.     }
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top