Multi-context operations
According to the previous chapter
In context, the definition of a single word is not exactly the same.
The single word in the code can be queried and defined in different contexts. To prove this, we will create an experiment here.
Step 1: I first use the Context Function to create their respective proprietary
Context. The context of apple is called Apple. There is only one word OS in it, which is defined as "iOS ". Google's context is called
Google, which has only one word OS, is defined as "android" (Android ). These two companies talk about operating system (OS)
System), it refers to different things, Apple
The OS is iOS, and Google is Android.
Step 2: I create two code blocks: Apple-fan and Google-fan.
[Print OS].
Step 3: Focus on Bind
Function to enable apple-fan to use Apple
Context allows Google-fan to adopt
Google
Context.
Step 4: Use Insert
Function, concatenates two pieces of code (the apple-fan and Google-fan
Put all content in the code
). In step 5, I confirm that the merge is correct. Step 6: execute this code to find the displayed knot
If they are inconsistent, it means that a piece of code can adopt different contexts.
For more information, see the slow action breakdown below.
Apple: context [OS: "iOS"]
Google: context [OS: "android"]
For ease of explanation, I assume that the preceding two command lines are executed together (like a command line ).
Before executing the above Code
The interpreter prepares apple, context, OS, and Google in the user context.
These four words, and from the Lib
Copy the context definition in the context (Lib
There are no apple, OS,
Google, so the three definitions cannot be copied ). After the code is executed, two new contexts are generated
User
Apple word and Google in context
The definition of a single word. The OS is left in the user context.
The definition is special
Value unset (indicating not set ).
Apple-Fan: [Print OS]
Google-Fan: [Print OS]
For ease of explanation, I assume that the preceding two command lines are executed together (like a command line ).
Before executing the above Code
The interpreter adds apple-fan, print, and googlefan in the user context.
These three words, and from the Lib
Copy the print definition in the context (Lib
The context does not contain apple-fan and
Google-fan
). After execution, the definitions of apple-fan and Google-fan are both code blocks and the content is both
Yes [Print OS]. Print
It is marked with a red triangle with the OS, indicating that they all use user
Context (red table)
.
Bind apple-fan Apple
Bind Google-fan Google
For ease of explanation, I assume that the preceding two command lines are executed together (like a command line ).
Before executing the above Code
The interpreter adds bind in the user context and
BIND in context
. After this code is executed, the code in apple-fan will be bound to Apple
Context, but apple
Context
Only OS is used, and no print is used.
Bound to Apple
Context, print
Maintain original binding
User
Context ). Similarly, the Code in Google-fan is only OS
Bound to Google
Context, print
Dimension
Hold the original binding (User context ).
Code: []
Insert Code apple-fan
Insert Code Google-fan
For ease of explanation, I suppose the above three command lines are executed together (like a command line ).
Before executing this code, REBOL
The interpreter adds code and insert in the user context, and
Context
Insert
Copy the definition. Code
The content points to a new code block, which contains four
Word, copied from Apple-fan and Google-fan. Note which contexts are used for distinguishing the four words here. By
On Google-fan
The code is later inserted into the square header (insert
Is inserted in the header), so the first OS
Its
Actually, it is from Google-fan.
Code
Do Code
For ease of explanation, I assume that the preceding two command lines are executed together (like a command line ).
Before executing this code, REBOL
The interpreter adds do in the user context and
Definition of do in context
Copy it. After this code is executed, Google
Context OS values ("android") and
OS in Apple Context
Value ("iOS ").
In the previous example, Google
Context and Apple
All contexts have OS. In this case, if we enter
OS.
But an error is returned, telling us the OS
No value. OS exists in the user context.
Is a special value unset (not set), which is equivalent to no value. Next, we set the OS
Set to "Windows ".
OS
It will be "Windows ".
How to get apple
Or Google
OS? We can add a path to achieve this:
Front
Based on its source context, and separated by a slash, it is written as Apple/OS and Google/OS.
The path is not limited to two layers and can have multiple layers. For example, system/options/home is the three-tier path.
Get REBOL
Main directory.
Let's look at another example. The PI (circumference rate) value was originally 3.14159265358979. Let's change it to a simpler one.
The value is 3.14. You don't have to worry about this exact value, so it disappears. All you have changed is the PI in the user context.
Replay
Version, the original pi
In the Lib context
You can get the original value by writing the path.
When writing code, we often need to bring together some values, for example, record the information of the same person together ["Tony"
1983-12-21]. To clearly express the meaning of the data, we usually add the field name in front of it
[Name: "Tony" birthday: 1983-12-21]. We usually wrap such data into objects ).
The object is written as follows:
Customer1: object [name: "Tony" birthday: 1983-12-21]
Customer2: object [name: "John" birthday: 1978-1-15]
There are two objects, whose data is separated separately. Don't worry, "John" will overwrite "Tony" because of their
Name
Is in different objects.
Does it feel like context? That's right! REBOL
In fact, it is to process context in the way of processing objects. Context and object
In REBOL
The language is not differentiated, and the function is the same. Generally, many words are put in the context, and objects are put in the context.
The number of words (fields) is relatively small. The Value in the context is not fixed and changes a lot. The value in the object is relatively fixed, just like the number
The fields in the database are the same. In-context functions account for the majority while in-object data.
In the process of program execution, you need to query the context frequently and find the corresponding definition of a single word to know how to execute it.
We have proved that the definition of a word can come from different contexts. Even with the same value, the actual execution may be different.
Meaning (subject to the function control before this value), each value may affect the meaning of subsequent values.
REBOL is the abbreviation of relative expression based objectlanguage, which means that this language allows you
Different expressions are made below (context. The same word can have different definitions and effects in different contexts.
It is related to the context, easier to understand, and easier to read and briefly understand the code.
This is a magic language, powerful, magical and advanced.
This article is excerpted from "programming ing: everyone can learn programming ".
Dr. Cai
Published by Electronic Industry Publishing House
Book details: http://blog.csdn.net/broadview2006/article/details/7768124