How are 'bind' and 'in' different?

49 Views Asked by At

Take the following code which creates a context:

c: context [a: 2]

You can see it creates a context c, and that a is not bound in the global context:

>> ?? c
c: make object! [
    a: 2
]
>> a
** Script Error: a has no value
** Near: a

Now if you use bind 'a c, it returns the value of the word in the context it was bound:

>> get bind 'a c
== 2

Which is also the same as in c 'a:

>> (get bind 'a c) = (get in c 'a)
== true

Looks like in is a version of bind with flipped arguments

So, how is in different?

There are some obvious feature additions in bind, like having a refinement /copy for efficiency, and also accepting a block! instead of a single word for its words argument.

In which case, the question becomes, why in?

Note

This was initially motivated by comments in this question, when I didn't quite understood what bind does, and a discussion on gitter prompted me to post this

1

There are 1 best solutions below

0
MarkI On

The big difference is that BIND can propagate the context of a word:

>> q: func [/local a b][a: 1 b: 2 return 'a]
>> get bind 'b q
== 2
>> get in q 'b
** Script Error: in expected object argument of type: object port
** Near: get in q 'b

Rebol 2 only of course.