oop - is there an object constructor in rebol -


i program functions in "instinctive" manner, current problem can solved objects, go ahead method.

doing so, trying find way give object constructor method, equivalent of init() in python, example.

i looked in http://www.rebol.com/docs/core-fr/fr-index.html documentation, couldn't find relevant.

there no special constructor function in rebol, there possibility write ad hoc init code if need on object's creation in spec block. example:

a: context [x: 123]  b: make [     y: x + 1     x: 0 ] 

so, if define own "constructor" function convention in base object, can call spec block on creation. if want make automatic, can wrap in function, this:

a: context [     x: 123     init: func [n [integer!]][x: n] ]  new-a: func [n [integer!]][make [init n]]  b: new-a 456 

a more robust (but bit longer) version of new-a avoid possible collision of passed arguments init object's own words be:

new-a: func [n [integer!] /local obj][              obj: make []         obj/init n ] 

you write more generic new function take base object first argument , automatically invoke constructor-by-convention function after cloning object, supporting optional constructor arguments in generic way more tricky.

remember object model of rebol prototype-based (vs class-based in python , other oop languages), "constructor" function gets duplicated each new object created. might want avoid such cost if creating huge number of objects.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -