2012-05-15

tim: Tim with short hair, smiling, wearing a black jacket over a white T-shirt (working)
2012-05-15 10:20 pm
Entry tags:

TMI: Destructors

Yesterday and today, I worked on adding destructors to classes. A destructor is a method that can't be named directly, to which the compiler inserts a call whenever the memory for the class is about to be freed (so, whenever its reference count becomes zero). Here's a super simple example of a class with a destructor:

class shrinky_pointer {
  let i: @@mut int;
  fn look_at() -> int { ret **(self.i); }
  new(i: @@mut int) { self.i = i; }
  drop { **(self.i) -= 1; }
}
Read more... )