Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace CONTEXT function(s) with OBJECT #2118

Open
rebolbot opened this issue Feb 28, 2014 · 5 comments
Open

Replace CONTEXT function(s) with OBJECT #2118

rebolbot opened this issue Feb 28, 2014 · 5 comments

Comments

@rebolbot
Copy link
Collaborator

Submitted by: BrianH

OBJECT was originally added to make code like this work:

>> object [a: b:]
== make object! [
    a: none
    b: none
]

Just a simple way to declare objects that are used as data structures, rather than code organization. The way it does this is to modify the spec (appending none) and calling MAKE OBJECT!, which again modifies the spec.

However, CONSTRUCT does the same thing:

>> construct [a: b:]
== make object! [
    a: none
    b: none
]

And it does it without modifying the spec (making it safe to reuse), and is native, so it's generally a better choice to use in these cases.

But then we have another problem, which is that the CONTEXT function is really badly named, since it doesn't really have anything to do with contexts. The Rebol-jargon term "context" is just the worst term to use for the referred-to concept, when dealing with a language that has real context-sensitive semantics (dialects). We would all benefit from that function going away, eventually.

And in a weird turn, there is a limited native CONTEXT function used during the bootstrap process, which is replaced at some point with the current mezzanine CONTEXT. Didn't see that coming.

So my request is that both the native and mezzanine CONTEXT be renamed to OBJECT, with their current behavior. CONTEXT would be left as a value alias of OBJECT for backwards compatibility, and marked as deprecated in the source, like FUNCT. See #2123 for that.

At some point soon we should unify the native and the mezzanine, since the single parameter means we can make it more optimized than MAKE OBJECT! (fewer semantic variants). Plus, the mezzanine OBJECT and the old CONTEXT are subject to #539, where the native isn't.

CC - Data [ Version: r3 master Type: Wish Platform: All Category: Mezzanine Reproduce: Always Fixed-in:none ]

@rebolbot
Copy link
Collaborator Author

Submitted by: fork

I never understood why OBJECT [a: b:] shouldn't be an error, so definitely support it being an error.

What I thought CONTEXT was for was to create a little local scope where you wouldn't write through to the external environment. But it only works for the top level set-words you pass it, as it's just calling make object!

x: y: 10 
result: context [x: 20 if true [y: 20] "something"] 
print x 
print y 
probe result

So that outputs "10" and "20" and "make object! [x: 20]". I think that given the properties of what it does, its name is deceptive. That behavior is defined pretty much by OBJECT already.

But that does suggest what would be useful, which would be something that output "10" and "10" and "something". I would call that a SCOPE.

@brianh has proposed an implementation of scope as:

scope: func [code [block!]] [do bind/copy/set code construct []] 
a: 1 b: 2 
probe scope [a: 10 if true [b: 20] reduce [a b]] 
print [a b]

...with the caveats that it binds SELF, and that it is subject to CC#539, until some mechanism for handling RETURN and EXIT properly from this kind of construct has been decided.

In StackOverflow chat we also discussed why making CONTEXT do what I thought it did (or coming up with another word) wouldn't be generally useful. For instance:

x: y: 10 
result: context [x: 20 if true [y: 20]] 
print result/x 
print result/y 

Wanting that code to print "10" and "10" requires deep collecting. Says @brianh: "We don't deep-collect set-words in modules either, because it hides errors and allocates unnecessary words in the module's object. Plus, most of those words get overridden by FUNCTION, CLOSURE, and nested objects anyways."

In general you don't want deep collection on entities you plan to keep around and treat like objects. It makes sense for SCOPE because it's not producing any kind of object, rather throwing the contents away when its done.

So it seems that OBJECT is "the better context", and CONTEXT can be deprecated without introducing a deep-copying variant.

@rebolbot
Copy link
Collaborator Author

Submitted by: BrianH

See #2119 for SCOPE. SCOPE is a proposal for a function that would do what CONTEXT was used for when the result was just thrown away afterwards (common in pre-module code). SCOPE does a better and safer job of that, with deep collection of set-words, and returns the value of the last expression, making it more useful when called within other code.

With this proposed change to OBJECT and the new SCOPE function, we could deprecate CONTEXT in both of its common uses (see #2123).

@rebolbot
Copy link
Collaborator Author

rebolbot commented Mar 5, 2014

Submitted by: BrianH

Implemented in rebol/rebol#199

@rebolbot
Copy link
Collaborator Author

rebolbot commented Mar 5, 2014

Submitted by: BrianH

It turns out that there are two CONTEXT functions, one mezzanine and one native. We should rename the native CONTEXT to OBJECT as well.

We might also consider filling in the missing functionality of the mezzanine in the native, then getting rid of the mezzanine altogether.

@rebolbot
Copy link
Collaborator Author

rebolbot commented Mar 7, 2014

Submitted by: BrianH

#2119 changed to WRAP, and the discovery of the native, mean that a new PR is needed. Back to reviewed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant