Tinderbox v11 Icon

String.try{expression(s)}[.thenTry{expression(s)}]


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Data Type Returned: 

Operator First Added: 

Operator in Current Baseline: 

Operator Last Altered: 

Operator Has Optional Arguments: 

 Function  [other Function type actions]

 Item  [operators of similar scope]

 Stream parsing  [other Stream parsing operators]

 boolean test

 v9.1.0

 Baseline

 As at baseline

 [More on optional operator arguments]


String.try{ expression(s) }[.thenTry{ expression(s) }]

This saves the value of a (String-type) attribute (the stream source) and evaluates .try{expression(s)}. It the evaluation fails, the app restores the string and allows it to processed it a different way in the chained .thenTry{expression(s)} operation. If all evaluations fail, the original value of String stream source is restored.

A .try{} may be followed by one or more .thenTry{} clauses. If the original .try{} succeeds the evaluated result is returned and all subsequent .thenTry{} clauses are ignored. If the original .try{} fails, then each .thenTry{} is attempted in turn, until a result is returned. If all expression evaluations fail, the original value of String is returned.

The current success/failure state can be tested using the .failed() operator.

For example:

$MyString.try{
   $MyString=$MyString.skipToWord("To:").captureWord;
}.thenTry{
   fail(); //signal an explicit failure and return a 'false' value to an enclosing expression
};

looks for strings such as "To:John Doe"

If the string "To:" is not found, $MyString will be unchanged. If found, $MyString's value is set to the word that follows, up to the next whitespace character or the end of the string, i.e. "John".


See also—notes linking to here: