Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // typedef names a type - consider it a forward declaration of a type name
- // of unknown kind
- // typedef always makes a distinct type - it is not an alias
- typedef exit_code;
- // one way to define a type is by making it a wrapper of another type
- // it is still a distinct type - no implicit conversion is allowed, no subtyping
- // relation is established, new type's namespace has no access to the privates
- // of the original type and vice versa.
- typedef exit_code(u32);
- // this type still has its own namespace, so we can put stuff in there
- // the keyword is WIP
- implementation exit_code{
- function is_success(code: exit_code): bool{
- return code.value == 0;
- }
- }
- // another way to define a type is struct definition
- // note that you cannot put any functions inside the struct - it is strictly
- // for data fields
- partial struct Rect{
- var x y: f32;
- // this lets you forward declare part of the struct
- // but the full definition must agree with all the partial definitions
- // in particular, the defintion of this form requires the full definiton
- // to have two floating point variables named x and y to be the first
- // two fields in the definition
- ...
- }
- struct Rect{
- var x y: f32;
- var width height: f32;
- // now the definition is complete
- }
- implementation Rect{
- // you may give definitions or declarations
- // ref is lifetime-checked non-owning non-nullable pointer
- function area(self: ref Rect): f32;
- function perimeter(self: ref Rect): f32{
- // you are allowed to use '.' on the `ref T` because it implements
- // the proxy[T] protocol - when the name lookup fails on the `ref T`
- // itself - and it does as `ref T` does not have any dependent names -
- // the search continues on the underlying type `T` (and the protocol
- // implementation describes how exactly to obtain a value of type `T`
- // from the type `ref T`)
- return (self.width + self.height) * 2;
- }
- }
- // you can provide definitions outside the implementation scope if a declartion
- // is given in the scope
- function Rect.area(self: ref Rect): f32{
- return self.width * self.height;
- }
- // you may open a namespace multiple times (they act exactly as if they were
- // one definition)
- implementation Rect{
- function expand(self: ref Rect, dx dy: f32){
- self.x -= dx;
- self.y -= dy;
- self.width += dx * 2;
- self.height += dy * 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement