<commit_before>\chapter{Frequently Asked Questions}
\label{sec:faq}

\section{Tools}
\paragraph{The server dies with ``stack exhaustion''}
Your program might be deeply recursive, or use large temporary
objects.  Use \option{--stack-size} to augment the stack size, see
\autoref{sec:tools:urbi}.

Note that one stack is allocated per ``light thread''.  This can
explain why programs that heavily rely on concurrency might succeed
where sequential programs can fail.  For instance the following
program is very likely to quickly exhaust the (single) stack.

\begin{urbifixme}
function consume (num)
{
  if (num)
    consume(num - 1) | consume(num - 1)
} |
consume (512);
\end{urbifixme}

But if you use \lstinline{&} instead of \lstinline{|}, then each
recursive call to \lstinline{consume} will be spawn with a fresh
stack, and therefore none will run out of stack space:

\begin{urbifixme}
function consume (num)
{
  if (num)
    consume(num - 1) & consume(num - 1)
} |
consume (512);
\end{urbifixme}

However your machine will run out of resources: this heavily
concurrent program aims at creating no less than $2^{513}$ threads,
about $2.68 \times 10^{156}$ (a 156-digit long number, by far larger
than the number of atoms in the observable universe, estimated to
$10^{80}$).

\section{\us}
\subsection{Objects lifetime}

\paragraph{How do I create a new Object derivative?}
Urbi is based on prototypes. To create a new Object derivative (which
will inherit all the Object methods), you can do:

\begin{urbiscript}
var myObject = Object.new;
[00000001] Object_0x76543210
\end{urbiscript}

\paragraph{How do I destroy an Object?}
There is no \lstinline{delete} in Urbi, for a number of reasons (see
\autoref{sec:k122:delete}).  Objects are deleted when they are no
longer used/referenced to.

In practice, users who want to ``delete an object'' actually want to
remove a slot --- see \autoref{sec:tut:objects}.  Users who want to
clear an object can empty it --- see \autoref{sec:k122:delete}.

Note that \lstinline{myObject = nil} does not explicitly destroy the
object bound to the name \lstinline{myObject}, yet it may do
so provided that \lstinline{myObject} was the last and only reference
to this object.

\subsection{Slots and variables}

\paragraph{How do I add a new slot in an object?}
To add a slot to an object \lstinline{O}, you have to use the
\lstinline{var} keyword, which is syntactic sugar for the
\lstinline{setSlot} method:

\begin{urbiscript}
var O2 = Object.new |
// Syntax...
var O2.mySlot1 = 42;
[00000001] 42

// and semantics.
O2.setSlot("mySlot2", 23);
[00000001] 23
\end{urbiscript}

Note that in a method, \lstinline{this} designates the current
object.  It is needed to distinguish the name of a slot in the current
object, versus a local variable name:

\begin{urbiscript}[firstnumber=last]
{
  // Create a new slot in the current object.
  var this.foo = 42;

  // Create a local variable, which will not be known anymore
  // after we exit the current scope.
  var bar = 23;
}|
bar;
foo;
[00000001] 42
\end{urbifixme}


\paragraph{How do I modify a slot of my object?}

Use the \lstinline|=| operator, which is syntactic sugar for the
\lstinline|updateSlot| method.

\begin{urbiscript}[firstnumber=last]
class O
{
  var mySlot = 42;
}|
// Sugarful.
O.mySlot = 51;
[00000001] 51

// Sugarfree.
O.updateSlot("mySlot", 23);
[00000001] 23
\end{urbiscript}

\paragraph{How do I create or modify a local variable?}
Use \lstinline|var| and \lstinline|=|.

\begin{urbiscript}[firstnumber=last]
// In two steps: definition, and initial assignment.
var myLocalVariable;
myLocalVariable = "foo";
[00000001] "foo"
// In a single step: definition with an initial value.<commit_msg>FAQ: match start and end tags.<commit_after>\chapter{Frequently Asked Questions}
\label{sec:faq}

\section{Tools}
\paragraph{The server dies with ``stack exhaustion''}
Your program might be deeply recursive, or use large temporary
objects.  Use \option{--stack-size} to augment the stack size, see
\autoref{sec:tools:urbi}.

Note that one stack is allocated per ``light thread''.  This can
explain why programs that heavily rely on concurrency might succeed
where sequential programs can fail.  For instance the following
program is very likely to quickly exhaust the (single) stack.

\begin{urbifixme}
function consume (num)
{
  if (num)
    consume(num - 1) | consume(num - 1)
} |
consume (512);
\end{urbifixme}

But if you use \lstinline{&} instead of \lstinline{|}, then each
recursive call to \lstinline{consume} will be spawn with a fresh
stack, and therefore none will run out of stack space:

\begin{urbifixme}
function consume (num)
{
  if (num)
    consume(num - 1) & consume(num - 1)
} |
consume (512);
\end{urbifixme}

However your machine will run out of resources: this heavily
concurrent program aims at creating no less than $2^{513}$ threads,
about $2.68 \times 10^{156}$ (a 156-digit long number, by far larger
than the number of atoms in the observable universe, estimated to
$10^{80}$).

\section{\us}
\subsection{Objects lifetime}

\paragraph{How do I create a new Object derivative?}
Urbi is based on prototypes. To create a new Object derivative (which
will inherit all the Object methods), you can do:

\begin{urbiscript}
var myObject = Object.new;
[00000001] Object_0x76543210
\end{urbiscript}

\paragraph{How do I destroy an Object?}
There is no \lstinline{delete} in Urbi, for a number of reasons (see
\autoref{sec:k122:delete}).  Objects are deleted when they are no
longer used/referenced to.

In practice, users who want to ``delete an object'' actually want to
remove a slot --- see \autoref{sec:tut:objects}.  Users who want to
clear an object can empty it --- see \autoref{sec:k122:delete}.

Note that \lstinline{myObject = nil} does not explicitly destroy the
object bound to the name \lstinline{myObject}, yet it may do
so provided that \lstinline{myObject} was the last and only reference
to this object.

\subsection{Slots and variables}

\paragraph{How do I add a new slot in an object?}
To add a slot to an object \lstinline{O}, you have to use the
\lstinline{var} keyword, which is syntactic sugar for the
\lstinline{setSlot} method:

\begin{urbiscript}
var O2 = Object.new |
// Syntax...
var O2.mySlot1 = 42;
[00000001] 42

// and semantics.
O2.setSlot("mySlot2", 23);
[00000001] 23
\end{urbiscript}

Note that in a method, \lstinline{this} designates the current
object.  It is needed to distinguish the name of a slot in the current
object, versus a local variable name:

\begin{urbiscript}[firstnumber=last]
{
  // Create a new slot in the current object.
  var this.foo = 42;

  // Create a local variable, which will not be known anymore
  // after we exit the current scope.
  var bar = 23;
}|
bar;
foo;
[00000001] 42
\end{urbiscript}


\paragraph{How do I modify a slot of my object?}

Use the \lstinline|=| operator, which is syntactic sugar for the
\lstinline|updateSlot| method.

\begin{urbiscript}[firstnumber=last]
class O
{
  var mySlot = 42;
}|
// Sugarful.
O.mySlot = 51;
[00000001] 51

// Sugarfree.
O.updateSlot("mySlot", 23);
[00000001] 23
\end{urbiscript}

\paragraph{How do I create or modify a local variable?}
Use \lstinline|var| and \lstinline|=|.

\begin{urbiscript}[firstnumber=last]
// In two steps: definition, and initial assignment.
var myLocalVariable;
myLocalVariable = "foo";
[00000001] "foo"
// In a single step: definition with an initial value.
{'fraction_non_alphanumeric': 0.10239827539746699, 'fraction_numerical': 0.034761519805982216, 'mean_word_length': 4.305932809149392, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 14, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7613242', 'n_tokens_mistral': 2546, 'n_tokens_neox': 2313, 'n_words': 986}