Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are OBJECT and CONTEXT synonyms in Rebol2?

My first assumption is that object and context are synonyms. So I created an object o and a context c. Probing them reports the same code construct, and also type is the same (both being type object!):

>> o: make object! [a: 1 b: 2]

>> c: context [a: 1 b: 2]

>> probe o
make object! [
    a: 1
    b: 2
]

>> probe c
make object! [
    a: 1
    b: 2
]

>> type? o
== object!

>> type? c
== object!

...but when testing for equality you get:

>> equal? o c
== false

So it seems clear they are not synonyms. Why not if they probe to be exactly the same?

like image 233
Pep Avatar asked Oct 01 '22 11:10

Pep


1 Answers

You did a context-to-object comparison, but you didn't do an object-to-object comparison! Would they test equal in Rebol2? Let's try...

>> equal? (make object! [a: 1]) (make object! [a: 1])
== false

Nope! equal? does not work in Rebol 2 on objects. For mysterious internal reasons we have no code for. :-/ In Rebol3 (which is open source) they do test equal, however:

>> equal? (make object! [a: 1]) (make object! [a: 1])
== true

Contexts will also test as equal to their corresponding object:

>> equal? (context [a: 1]) (object [a: 1])      
== true

I first found out about the distinction between object and context in Rebol3 when I noticed object was a mezzanine that modified its input block. This puzzled me.

>> source object
object: make function! [[
    "Defines a unique object."
    blk [block!] "Object words and values (modified)"
][
    make object! append blk none
]]

>> source context
context: make function! [[
    "Defines a unique object."
    blk [block!] "Object words and values (modified)"
][
    make object! blk
]]

The obvious consequence is that you can make an object which has no terminal value, like object [a: b: c:] whereas with a context you would have to write context [a: b: c: none] to keep it from being an error.

(Note: I'm actually not sure why having that be an error case for context is so important, or alternately why having it not be an error case for object is so important. It seems to me that "make object!" could just pick one internal expectation and stick to it--then get rid of the CONTEXT word and everyone is a little less confused. Perhaps someone will have a comment to clarify that?)

like image 74
HostileFork says dont trust SE Avatar answered Oct 05 '22 11:10

HostileFork says dont trust SE