Dialogs

Within a standard interactive session, you can create "subsessions" or dialogs using the Wolfram Language command (RefLink[Dialog, paclet:ref/Dialog]). Dialogs are often useful if you want to interact with the Wolfram Language while it is in the middle of doing a calculation. As mentioned in "Tracing Evaluation", (RefLink[TraceDialog, paclet:ref/TraceDialog]) for example automatically calls (RefLink[Dialog, paclet:ref/Dialog]) at specified points in the evaluation of a particular expression. In addition, if you interrupt the Wolfram Language during a computation, you can typically "inspect" its state using a dialog.

Initiating and returning from dialogs.

This initiates a dialog:

Dialog[]

You can do computations in a dialog just as you would in any Wolfram Language session:

2^41

You can use (RefLink[Return, paclet:ref/Return]) to exit from a dialog:

Return[]

When you exit a dialog, you can return a value for the dialog using (RefLink[Return, paclet:ref/Return])[expr]. If you do not want to return a value, and you have set (RefLink[$IgnoreEOF, paclet:ref/$IgnoreEOF])=(RefLink[False, paclet:ref/False]), then you can also exit a dialog simply by giving an end-of-file character, at least on systems with text-based interfaces.

To evaluate this expression, the Wolfram Language initiates a dialog:

1+Dialog[]^2

The value a+b returned from the dialog is now inserted in the original expression:

Return[a+b]

In starting a dialog, you will often find it useful to have some "initial expression". If you use (RefLink[Dialog, paclet:ref/Dialog])[expr], then the Wolfram Language will start a dialog, using expr as the initial expression, accessible for example as the value of %.

This first starts a dialog with initial expression a^2:

Map[Dialog,{a^2,b+c}]

% is the initial expression in the dialog:

%^2+1

This returns a value from the first dialog, and starts the second dialog, with initial expression b+c:

Return[%]

This returns a value from the second dialog. The final result is the original expression, with values from the two dialogs inserted:

Return[444]

(RefLink[Dialog, paclet:ref/Dialog]) effectively works by running a subsidiary version of the standard Wolfram Language main loop. Each dialog you start effectively "inherits" various values from the overall main loop. Some of the values are, however, local to the dialog, so their original values are restored when you exit the dialog.

Thus, for example, dialogs inherit the current line number (RefLink[$Line, paclet:ref/$Line]) when they start. This means that the lines in a dialog have numbers that follow the sequence used in the main loop. Nevertheless, the value of (RefLink[$Line, paclet:ref/$Line]) is local to the dialog. As a result, when you exit the dialog, the value of (RefLink[$Line, paclet:ref/$Line]) reverts to what it was in the main loop.

If you start a dialog on line 10 of your Wolfram Language session, then the first line of the dialog will be labeled (RefLink[In, paclet:ref/In])[11]. Successive lines of the dialog will be labeled (RefLink[In, paclet:ref/In])[12], (RefLink[In, paclet:ref/In])[13], and so on. Then, when you exit the dialog, the next line in your main loop will be labeled (RefLink[In, paclet:ref/In])[11]. At this point, you can still refer to results generated within the dialog as (RefLink[Out, paclet:ref/Out])[11], (RefLink[Out, paclet:ref/Out])[12], and so on. These results will be overwritten, however, when you reach lines (RefLink[In, paclet:ref/In])[12], (RefLink[In, paclet:ref/In])[13], and so on in the main loop.

In a standard Wolfram Language session, you can tell whether you are in a dialog by seeing whether your input and output lines are indented. If you call a dialog from within a dialog, you will get two levels of indentation. In general, the indentation you get inside d nested dialogs is determined by the output form of the object DialogIndent[d]. By defining the format for this object, you can specify how dialogs should be indicated in your Wolfram Language session.

Options for (RefLink[Dialog, paclet:ref/Dialog]).

Whatever setting you give for (RefLink[DialogSymbols, paclet:ref/DialogSymbols]), (RefLink[Dialog, paclet:ref/Dialog]) will always treat the values of (RefLink[$Line, paclet:ref/$Line]), (RefLink[$Epilog, paclet:ref/$Epilog]), and (RefLink[$MessageList, paclet:ref/$MessageList]) as local. Note that if you give a value for (RefLink[$Epilog, paclet:ref/$Epilog]), it will automatically be evaluated when you exit the dialog.

When you call (RefLink[Dialog, paclet:ref/Dialog]), its first step is to localize the values of variables. Then it evaluates any expression you have set for the option (RefLink[DialogProlog, paclet:ref/DialogProlog]). If you have given an explicit argument to the (RefLink[Dialog, paclet:ref/Dialog]) function, this is then evaluated next. Finally, the actual dialog is started.

When you exit the dialog, you can explicitly specify the return value using (RefLink[Return, paclet:ref/Return])[expr]. If you do not do this, the return value will be taken to be the last value generated in the dialog.