3.1 Hello World!
Last updated
Last updated
For an application to progress, the state of its contexts must be changed. Operations are the constructs that, at a high-level, define when those contexts are changed/created, and at a more detailed level, how they are changed.
In general, a simple operation that has no required contexts has a declaration that looks like :
Qualifications are where the 'when' aspect of Contextual Programming is defined. Statements after operation
and before ?
are all considered qualifications and often take the form of when "some condition"
, but they can also have other forms that will be shown later.
Operation logic is a series of statements that either create new contexts or directly manipulate the operation's contexts (if it has any). The operation logic is actually optional, leaving the operation name and its qualifications as the only required parts of an operation. Such qualification-only operations are usually used in a hierarchy, through a kind of inheritance, to add qualifications to other operations.
One of the simplest examples of an operation is Rede's () implementation of the classic "Hello World!" program:
In , Output Hello World :: operation when initialized?
, there's the operation name, Output Hello World
, and one qualification, when initialized
. This qualification is referred to as a qualification. When applied in this way, a when initialized
will qualify for execution when the application starts, so this operation called Output Hello World
will execute immediately when the application begins to execute.
The third line, evaluate "Hello World!" as Console Message.
, is the operation logic. This is what will actually be executed when Output Hello World
qualifies and executes. There's a lot going on in this line:
evaluate
is a command to the . It tells the runtime to perform operations that are qualified by the context(s) that follows it. evaluate
will require the runtime to perform any such operations before the current operation continues, unlike its counterpart \evaluate
, which is not concerned about when the runtime will perform the operations. There are circumstances when \evaluate
is more appropriate than evaluate
, primarily when what is expected to be accomplished by \evaluate
is of no importance to the current chain of logic or should be done entirely .
"Hello World!"
is a string literal, which is to say it is an instance of a type. This is defining the text that will be presented to the user.
as Console Message
is a command to the string "Hello World!"
as a Console Message
context type, which is a built-in context provided by the runtime. This will be done prior to the evaluate
command being evaluated by the runtime.
Altogether, evaluate "Hello World!" as Console Message.
is telling the application runtime to "take the text, 'Hello World!', alias it as a Console Message, and execute any operations that qualify for that context". The standard functionality of the runtime includes an operation that will qualify for a single Console Message
and will perform logic that will result in Hello World!
being displayed to the user through a console.
While not much, this operation shows how to send messages to the user through the console while following the practices of Contextual Programming.
Most operations will require at least one context, as the purpose of most operations is to progress the application's state. To specify the required contexts, the operation declaration changes some to look like :
The [Context Types] that appears in the operation's description defines the number of the operation's parameters (the "Context Names" above).
The Hello World example can be modified to greet the user. One way to accomplish this is with a new context and an operation to define its state. It's not necessary to do it this way, but this implementation is clearer and more reusable.
Next, the updated initial operation:
There are a few things different here. The third line is noteworthy in that it is evaluating an operation (which is defined below) but is doing so while defining a new context. The context will be referred to as name
and will be of the new Input User Name
type, and it will be immediately evaluated.
The other difference is on the fourth line (which is wrapped to the fifth line for easier viewing). This line is similar to the previous evaluate "Hello World!" as Console Message
but with some other code nested inside the string. In Rede \( )
denotes additional strings to be formatted within the encapsulating string. The values within the parentheses are basically injected into the string. In this case, if first name
of name
is "A" and last name
of name
is "B", then the resulting string will be formatted as "Hello A B!" which will then be output to the console.
Finally, the new operation that will populate the evaluated Input User Name
:
Note the punctuation ending each line in this example. Any time a construct is declared, it must end with a period (.
), except when it is nested within another construct, in which it ends with a semi-colon (;
). Any value, qualification, or logic statements within a set of such elements of a declaration, must end with a comma (,
). This structure is intended to be closely related to the use of punctuation in English.
Punctuation is not required to stack in Rede, so .
supersedes ;
which supersedes ,
.
Unlike the initial operation, the only qualification for this operation is that there is an Input User Name
being evaluated, which will be named input name
within the scope of this operation. There's a number of evaluates that are similar to what has been seen so far. Console Response
is the simplest way in Rede to obtain an input through the console and is basically an alias for a String
, so that is used to receive the user's first and last names.
The only particularly different part of this operation is the last line, where the responses from the user are assigned as the first name
and last name
values of the Input User Name
. The assignment is performed stepping left-to-right in both the group of parameters for input name
(to the left of is
) and the collection composed of first name
and last name
(to the right of is
). This line is what populates the Input User Name
with the user's input first and last names.
Altogether, this new context and these operations will request the first and last name of the user and then output a greeting to them.
First, the new :