Skip to content

Metaclass that provides basic garbage collection for instances. It does this by tying the instance lifetime to a variable - when the variable goes out of scope, is unset, or overwritten, the object is automatically destroyed

License

Notifications You must be signed in to change notification settings

RubyLane/gc_class

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GC_CLASS

Metaclass providing basic object garbage collection using variable traces

Usage

gc_class create Foo {
	variable thing

	constructor {a_thing} {
		set thing	$a_thing
	}

	method greet {} {
		puts "hello, $thing"
	}
}

proc bar {} {
	Foo instvar foo1 "first example"
	$foo1 greet		;# says "hello, first example"

	Foo instvar foo2 "second example"
	$foo2 greet		;# says "hello, second example"

	Foo instvar foo2 "third example"	;# second example dies here
	$foo2 greet		;# says "hello, third example"

	Foo instvar foo4 "fourth example"
	$foo4 greet		;# says "hello, fourth example"

	set foo2	"something else"	;# third example dies here

	unset foo4		;# fourth example dies here

	Foo instvar ::foo5 "fifth example"

	# first example dies when the proc returns here (since $foo1 goes out of scope)
}

bar

# One Foo instance remains, the "fifth example"

$foo5 greet		;# says "hello, fifth example"
unset foo5		;# fifth example dies here

# No instances of Foo remain

That's about it really. Everything else should work the same as objects created directly from oo::class.

License

This package is licensed under the same terms as the Tcl core.

About

Metaclass that provides basic garbage collection for instances. It does this by tying the instance lifetime to a variable - when the variable goes out of scope, is unset, or overwritten, the object is automatically destroyed

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages