• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[Rebol School] REBOL School

MarcS
12-Oct-2012
[1347]
I see that some folks use 'copy' on top-level bindings (i.e., foo: 
copy [], bar: copy "", bas: copy #{}). Is this simply stylistic (for 
consistency with initialisation inside of blocks) or am I overlooking 
a potential pitfall?
Henrik
12-Oct-2012
[1348x3]
The pitfall is the copy trap.
and the copy trap may occur anywhere.
http://www.rebol.net/wiki/Forgoing_faux_pas#The_COPY_Trap
MarcS
12-Oct-2012
[1351x6]
sure, but that example is inside a block
( f: has [t] [t: [] append t 1] )
i'm asking why (if) i should do:
rebol [] foo: copy []
instead of
rebol [] foo: []
Henrik
12-Oct-2012
[1357x2]
it does not matter. it occurs anywhere.
If you use foo: [], you can.


There are perfectly legitimate ways to use the block that way. You 
should simply be aware of this fact, when assigning the word to that 
block.
MarcS
12-Oct-2012
[1359x4]
so is there any reason for using copy at the top level (global assignments) 
with binding to literals?
(i.e., i'm asking about 'foo: [1 2 3]' rather than 'bar: [1 2 3] 
foo: copy bar')
i guess a related question is whether i'll run into difficulties 
with 'context [ foo: [] ]' versus 'context [ foo: copy [] ]'
(typo: 'with binding' should read 'when binding')
Henrik
12-Oct-2012
[1363x3]
that again depends on how the word is meant to be used. if you use 
the context as a copyable prototype, then the original 'foo block 
will be re-used, if you don't copy it on creation of the context.
at the top level, there may not be a reason, but through various 
tricks, it's still possible that it may be relevant to copy it, such 
as if the code itself is later used in a context, which means the 
code is no longer top level.
if you don't copy it on creation of the context.

 => "if you don't copy it on creation of each new copy of the original 
 context."
MarcS
12-Oct-2012
[1366x3]
hmm
maybe i'm misunderstanding your clarification, but i don't see any 
difference between copying the literal block: http://pastebin.com/L2Qx4djY
oops, 'when copying the literal block'* (sorry for all the typos)
Henrik
12-Oct-2012
[1369x2]
hmm... I clearly remember it being not copied, but I seem to be wrong 
about that.
aha, it's the case for contexts inside contexts.
Ladislav
12-Oct-2012
[1371]
so is there any reason for using copy at the top level (global assignments) 
with binding to literals?
 - certainly, there are reasons. See these two examples:

; example #1
repeat i 2 [
    a: []
    append a i
    print mold a
]

and

; example #2
repeat i 2 [
    a: copy []
    append a i
    print mold a
]
MarcS
12-Oct-2012
[1372]
ladislav: i meant when not binding inside a block
Ladislav
12-Oct-2012
[1373x2]
What does it have in common with "binding"?
BTW, the examples both use binding in a way
MarcS
12-Oct-2012
[1375]
i just mean assigment
Ladislav
12-Oct-2012
[1376]
actually, you cannot use assignment "outside of a block" in REBOL
MarcS
12-Oct-2012
[1377]
henrik: but - http://pastebin.com/3s30AxY4
Ladislav
12-Oct-2012
[1378]
(there is always a block somewhere)
MarcS
12-Oct-2012
[1379x2]
ladislav: sorry, i'm using terminology sloppily
i guess: assigning to a word in the global (system?) context
Ladislav
12-Oct-2012
[1381x2]
that is what both the examples above do
(the 'A word is not local)
MarcS
12-Oct-2012
[1383]
yes, but the other part was: not inside a(nother literal) block
Ladislav
12-Oct-2012
[1384]
That cannot be done.
MarcS
12-Oct-2012
[1385]
okay, not inside another explicit literal block
Henrik
12-Oct-2012
[1386]
MarcS, yes, now you are in X and Y pointing at the same context.
Ladislav
12-Oct-2012
[1387]
In REBOL, there is always a block somewhere
MarcS
12-Oct-2012
[1388x2]
henrik: oh, right
ladislav: yes, okay - i'm just saying that i understand the pitfalls 
of not copying when you're re-entering a block (be it a loop, function, 
etc. body)
Ladislav
12-Oct-2012
[1390]
for example, something like:

do "a: []"

first "converts" the string to a block
MarcS
12-Oct-2012
[1391]
but where i'm assigning to a "global" (which i understand is just 
another context) outside of an explicit (one i write myself) literal 
block, are there pitfalls in not using copy
Henrik
12-Oct-2012
[1392]
yes, if you are running the same script several times in the same 
console.
MarcS
12-Oct-2012
[1393x4]
also, i get that x: [ append [] 1 ] then 'do x do x' differs from 
x: [ append copy [] 1 ] 'do x do x'
henrik: so if i load the script, then do it, rather than doing the 
file?
makes sense, hadn't considered that
so: best practice is to consider everything to be inside a block, 
and hence copy even toplevel literals?