Skip to content

Instantly share code, notes, and snippets.

@joseoliv
Last active June 13, 2019 22:21
Show Gist options
  • Save joseoliv/ea6ed3311f3fcfb54bfe035d3e400a88 to your computer and use it in GitHub Desktop.
Save joseoliv/ea6ed3311f3fcfb54bfe035d3e400a88 to your computer and use it in GitHub Desktop.
countNew
package metaobjectTest
@doc{*
Annotation 'countNew' should be attached to a 'init' or 'init:' method.
It increments a global variable each time the method is called. When
attached to all 'init' or 'init:' methods of a prototype, it counts
how many objects were created. This number is put in a table called
"CountNew" whose key is the prototype name (with the package). See
the example below.
*}
object CountNew
@countNew
func init {
}
@countNew
func init: Int n { }
func run {
assert numberOfObjectsCreated == 0;
var cn1 = CountNew();
assert numberOfObjectsCreated == 1;
var cn2 = CountNew new: 2;
assert numberOfObjectsCreated == 2;
var cn3 = CountNew new;
assert numberOfObjectsCreated == 3;
}
func numberOfObjectsCreated -> Int {
cast table = (System globalTable get: "CountNew") {
type table get: "metaobjectTest.CountNew"
case Int count {
return count
}
}
return 0
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment