Skip to content

Instantly share code, notes, and snippets.

@fjolnir
Created February 28, 2013 09:02
Show Gist options
  • Save fjolnir/5055344 to your computer and use it in GitHub Desktop.
Save fjolnir/5055344 to your computer and use it in GitHub Desktop.

scope entry/exit handlers

-- Simple object system with automatic reference counting
type Object': refCount=>Integer;
type Object => *Object';

Object.init: self;
Object.dealloc: free self;
Object.retain: incr refCount;
Object.release: if ((decr refCount) = 0) then {
    dealloc!;
    true
} else `false`;
Object.__enter: self.retain!;
Object.__exit: self.release!;

import calloc Integer, Integer->(mut Char)*;
import free (mut Char)*->Void;
macro #new T: {
    obj: calloc 1, (sizeof T);
    obj.init!
}

type Person => Object: name => mut String;
Person.dealloc: {
    if (name ~= null) then `free name`;
    super!
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment