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

Just two ways to the same place?

 [1/10] from: kpeters::otaksoft::com at: 27-Aug-2007 17:26


Is there any difference between set 'some-word func [ ...... and some-word: func[ ..... Thanks, Kai

 [2/10] from: gregg::pointillistic::com at: 27-Aug-2007 19:50


> Is there any difference between > > set 'some-word func [ ...... > > and > > some-word: func[ .....
If you do both of those inside a context, the first will set the word in the global context; the second will set it inside the context. Using 'set in a context is an easy way to "export" words for public use. --Gregg

 [3/10] from: tim-johnsons::web::com at: 28-Aug-2007 7:47


On Monday 27 August 2007, Kai Peters wrote:
> Is there any difference between > > set 'some-word func [ ...... > > and > > some-word: func[ .....
Hi Kai: Consider this: context [set 'test func[][]] ;; untested :-) here we have an anonymous context. And such a creature can be very useful. tim

 [4/10] from: kpeters:otaksoft at: 28-Aug-2007 21:33


Thanks Gregg ~ had a feeling that there was a good reason there... Kai On Mon, 27 Aug 2007 19:50:54 -0600, Gregg Irwin wrote:

 [5/10] from: kpeters::otaksoft::com at: 28-Aug-2007 21:35


Hi Tim ~ tried to think of a situation where I'd use that and could not come up with anything - do you have a practical example you can share? Thanks Kai On Tue, 28 Aug 2007 07:47:45 -0800, Tim Johnson wrote:

 [6/10] from: tim-johnsons:web at: 29-Aug-2007 7:37


On Tuesday 28 August 2007, Kai Peters wrote:
> Hi Tim ~ > > tried to think of a situation where I'd use that and could > not come up with anything - do you have a practical example > you can share?
:-) I use many. The code below has been modified to remove any dependencies, so it may break when you test it, but any error should be obvious, and hopefully the concept is clear. Data stored in the anonymous context is presistant, I use it to create html buttons with inline javascript for rollover effects. It has two helpful characteristics: 1)"Dictionary style" argument list. 2)No need to redundantly call with a large 'args list context [ type: "button" name: "button" value: "click me" bgc: "#cccccc" color: "black" hcolor: "lightblue" fs: "12px" fw: "normal" hbgc: "black" onclick: "" ;; previous defined words kws: [type name value bgc color hbgc hcolor fs fw onclick] set 'do-button1 func[ "create a button using 'button function with keywords" [catch] args[block!] "keywords: type, name, value, bgc, color, hbgc, hcolor, fs, fw, onclick" /local use-onclick test blk][ foreach[key val] args[ if not in self key [throw make error! rejoin["Unknown key: " form key ". Use one of: [" form kws "]"]] if key = 'onclick[use-onclick: true] either word? val[ set in self key get val ][set in self key val] ] blk: copy ["<input type=^"" self/type "^" name=^"" self/name "^" value=^"" self/value "^" style=^"background-color: " self/bgc "; color: " self/color "; font-weight: " self/fw "; font-size: " self/fs ";^" onmouseout=^"this.style.backgroundColor='" self/bgc "';this.style.color='" self/color "'^" onmouseover=^"this.style.backgroundColor='" self/hbgc "';this.style.color='" self/hcolor "'^"" ] if use-onclick[ append blk [" onclick=^"" self/onclick "^"" ] ] append blk ">" ] ]

 [7/10] from: moliad::gmail::com at: 29-Aug-2007 12:28


Hi Kai, The concept of anonymous context is an old one in REBOL. This is a sign that you are starting to delve deeper into rebolese :-) The most obvious reason one builds such contexts is to protect functions and local data from spilling into the global context (and vice-versa). since you have no need for the context itself, there is no need to store any reference to it. You can sort of see this as a manual "scoping" of variables (although its really static binding of word). Because REBOL's garbage collector will not release memory until all references of any context or its members are set to none (this is a quick summary of how it works, there is a bit more to it), the anonymous context will stay in RAM while its used. -MAx On 8/29/07, Kai Peters <kpeters-otaksoft.com> wrote:

 [8/10] from: tim-johnsons::web::com at: 29-Aug-2007 9:25


On Wednesday 29 August 2007, Maxim Olivier-Adlhoch wrote:
> The most obvious reason one builds such contexts is to protect functions > and local data from spilling into the global context (and vice-versa). > since you have no need for the context itself, there is no need to store > any reference to it. You can sort of see this as a manual "scoping" of > variables (although its really static binding of word).
To expand on that, any data stored in an anonymous context can only be referenced and/or changed by a function within that context. This makes the data *truly* private! I think one can implement 'use in a similar fashion. I've seen examples on this ML - if memory serves me - but not implemented that approach myself. Tim

 [9/10] from: kpeters:otaksoft at: 29-Aug-2007 20:04


Tim & Max ~ so then the script I just wrote below would not be possible with an anonymous context, right? Kai rebol [] ;---------------------------------------------- selector: center-face layout [ btn "Module 1" [ unview view/new module-1 ] btn "Module 2" [ unview view/new module-2 ] ; btn "# of calls to Module 1" [ notify form m1/c ] btn "# of calls to Module 2" [ notify form m2/c ] ] ;---------------------------------------------- m1: context [ c: 0 set 'module-1 does [ center-face layout [ text "Module #1" btn "Back" [ c: c + 1 unview view/new selector ] ] ] ] ;---------------------------------------------- m2: context [ c: 0 set 'module-2 does [ center-face layout [ text "Module #2" btn "Back" [ c: c + 1 unview view/new selector ] ] ] ] ;---------------------------------------------- view selector ;-------------------- EoS ---------------------

 [10/10] from: moliad::gmail::com at: 30-Aug-2007 0:11


just for the exercise, here is your previous script using anonymous context. In such an example its not super usefull, but gives the idea. its often used when people define libs, where they want to protect the internals from accidental tampering... and some developers do not like the path notation or object/method concept, so they put their code in the global namespace on purpose, for no other reason than not having to type lib/function all the time. good and bad... depends on size and scope of your project... especially when several people and parts are intertwined... in the following, the only way to tamper with c is to actually modify (using change or insert for example on second :m1c ) or somehow copy & rebind part of the m1c function body... but all of this is pretty advanced and is only going to be done on purpose... so bottom line, it does protect your code from "unintentional" havoc, but real malicious tampering is not really preventible. rebol [] ;---------------------------------------------- selector: center-face layout [ btn "Module 1" [ unview view/new module-1 ] btn "Module 2" [ unview view/new module-2 ] ; btn "# of calls to Module 1" [ m1c ] btn "# of calls to Module 2" [ m2c ] ] ;---------------------------------------------- context [ c: 0 set 'm1c does [ notify form c ] set 'module-1 does [ center-face layout [ text "Module #1" btn "Back" [ c: c + 1 unview view/new selector ] ] ] ] ;---------------------------------------------- context [ c: 0 set 'm2c does [ notify form c ] set 'module-2 does [ center-face layout [ text "Module #2" btn "Back" [ c: c + 1 unview view/new selector ] ] ] ] ;---------------------------------------------- view selector ;-------------------- EoS ---------------------