Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: How to remove words from objects?

From: maximo:meteorstudios at: 13-Jan-2004 13:25

did you know you do not need a real object for object-like behaviour. This is not the solution to everything, but well used, it can be as effective. note that in the following, there is no context. and no 'self word. (you could define it, but I'll leave that little part as an exam. Actually, one solution is included later ;-) in short, just use a block which looks like the pairs of words and values you use to define objects, but don't use set-word (word:) just simple words (word) . blk: [ a 1 b 2 ]
>> blk/a
== 1
>> blk/b
== 2 you can also add functions to it although its a little more complicated, but their usage is as easy. blk: compose [ a 1 b 2 pr (func [arg1][print [arg1 "...done!"]]) ] note the use of compose and the outer parenthesis surrounding the function definition to use it though its as easy
>> blk/pr "hello world"
hello world ...done! you can also assign values to the block like you would with objects: blk/a: 44 when blocks are used for simple data storage, used properly, this trick makes blocks behave almost identically like objects. With the difference that you can edit them much more easily and that any references to them becomes safe. adding a new thing to the object: append blk [thing "thang"] probe blk [ a 1 b 2 pr (func [arg1][print [arg1 "...done!"]]) thing "thang" ] but again, remember that there is no context, so functions do have limits unless you really tear up your rebol mind and start doing run-time binding.. which is not the easier task... you can also add an argument to all functions called 'self, which then mimics the useage of self. I this case, when calling a function in the block, just supply the block as the first parameter of the function call... with the tone of your mail, it seems that you are rather new to rebol. if this is the case then Welcome!, and I hope this example is a true testatment to how rebol can be used in many different ways. Also note that rebol recycles most constructs you have learned, instead of forcing you to learn completely new constructs for each different task. for example, arguments, blocks of data, object specs code blocks, are all built up of BLOCK, which can be expressed as rebol encounters them. in other languages, these are all built up of different (similar) datatypes each allowing ONLY a (small?) subset of the capabilities which they all share. HTH! -MAx --- You can either be part of the problem or part of the solution, but in the end, being part of the problem is much more fun.