Hello,

Load some vocabs

( scratchpad ) USING: namespaces.private namespaces.lib mirrors ;
Loading P" resource:extra/namespaces/lib/lib.factor"
:warnings - print 6 semantic warnings.
----------
----------

Define a class

( scratchpad ) TUPLE: rgb red green blue ;
----------
----------

Create an instance

( scratchpad ) 10 20 30 rgb boa
----------
T{ rgb f 10 20 30 }
----------

Time for the magic. Let's make a mirror out of the tuple. Then let's stick it 
on the namestack.

We can't just stop there though. A mirror obeys some of the table protocol, 
but not all. In order for a datastructure to participate as a member of the 
namestack, it needs allow for new elements to be added. We work around this 
limitation of mirrors by immediatly putting a hashtable on the stack. This 
way, if the user adds something to the namespace, it's added to that 
hashtable.

( scratchpad ) dup <mirror> >n H{ } >n
----------
T{ rgb f 10 20 30 }
----------

OK. Let's read the "red" variable from the namespace. We should get the 
corresponding slot value.

( scratchpad ) "red" get .
10
----------
T{ rgb f 10 20 30 }
----------

What about setting a slot? Well, our mirror isn't at the top of the namestack. 
Remember, we shadowed it with that hashtable. The 'set' word manipulates the 
namespace at the top of the namestack.

So how do we hit namespaces that are not at the top? There's a word 
called 'set*' in namespaces.lib which does this. Let's try it:

( scratchpad ) 100 "red" set*
----------
T{ rgb f 100 20 30 }
----------
( scratchpad )

        Bingo.

In this example, I left the tuple on the stack so we could see the value 
updated. But of course, it doesn't have to be on the stack.

Ed

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to