At sign (@) on right side of assignment: Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (Created page with "<!-- page name: At sign (@) on right side of assignment --> {{Template: New feature in 7.9 mods}} Some programming languages have operators which allow the right-hand side of an ...")
 
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:
{{Template: New feature in 7.9 mods}}
{{Template: New feature in 7.9 mods}}
Some programming languages have operators
Some programming languages have operators
which allow the right-hand side of an assignment statement to reference
that allow the right-hand side of an assignment statement to reference
the left-hand side.  In C, for example, <code>i += 2</code> is equivalent to <code>i = i + 2</code>.
the left-hand side.  In C, for example, <code>i += 2</code> is equivalent to <code>i = i + 2</code>.
   
   
Line 22: Line 22:
   
   
The <code>@</code> character is used in an expression on the right side of an
The <code>@</code> character is used in an expression on the right side of an
assignment statement wherever a "value" phrase may occur, e.g., a variable,
assignment statement wherever a "value" phrase may occur, for example, a variable,
literal, method expression, $function, or sub-expression.  The <code>@</code>
literal, method expression, $function, or sub-expression.  The <code>@</code>
portion of the expression is called the "<code>@</code> phrase".
portion of the expression is called the "<code>@</code> phrase."
   
   
==Examples==
==Examples==
Line 30: Line 30:
Some examples of the <code>@</code> phrase are:
Some examples of the <code>@</code> phrase are:
<table>
<table>
<tr><td>Add  2 to a <var>Float</var> variable:
<tr><td>Add  2 to a <var>Float</var> variable:</td>
<p class="code">%x = @ + 2</p>
<td><p class="code">%x = @ + 2</p>
</td></tr>
</td></tr>
   
   
<tr><td>Raise a <var>Float</var> variable to its own power:
<tr><td>Raise a <var>Float</var> variable to its own power:</td>
<p class="code">%x = @:toPower(@)</p>
<td><p class="code">%x = @:toPower(@)</p>
</td></tr>
</td></tr>
   
   
<tr><td>Code with long variable names can become much more readable:
<tr><td>Code with long variable names can become much more readable:</td>
<p class="code">%numberOfCustomerInteractions = @ + 1
<td><p class="code">%numberOfCustomerInteractions = @ + 1
%totalTimeUsedForTransaction = @ + %time
%totalTimeUsedForTransaction = @ + %time</p>
   
   
<nowiki>* As opposed to:</nowiki>
As opposed to:
   
   
%numberOfCustomerInteractions = %numberOfCustomerInteractions + 1
<p class="code">%numberOfCustomerInteractions = %numberOfCustomerInteractions + 1
%totalTimeUsedForTransaction = %totalTimeUsedForTransaction + %time
%totalTimeUsedForTransaction = %totalTimeUsedForTransaction + %time
</p>
</p>
</td></tr>
</td></tr>
   
   
<tr><td>Concatenate to string:
<tr><td>Concatenate to string:</td>
<p class="code">%foo = @ with '.suffix'</p>
<td><p class="code">%foo = @ with '.suffix'</p>
</td></tr>
</td></tr>
   
   
<tr><td>Concatenate to lowercased string:
<tr><td>Concatenate to lowercased string:</td>
<p class="code">%foo = 'prefix.' with @:toLower</p>
<td><p class="code">%foo = 'prefix.' with @:toLower</p>
</td></tr>
</td></tr>
   
   
<tr><td>Get number plus next prime number:
<tr><td>Get number plus next prime number:</td>
<p class="code">%foo = @ + @:nextPrime</p>
<td><p class="code">%foo = @ + @:nextPrime</p>
</td></tr>
</td></tr>
   
   
<tr><td>Map variable using <var>NamedArraylist</var>:
<tr><td>Map variable using <var>NamedArraylist</var>:</td>
<p class="code">%foo = %namedArrlis(@)</p>
<td><p class="code">%foo = %namedArrlis(@)</p>
</td></tr>
</td></tr>
   
   
<tr><td>Get next item in linked list of objects:
<tr><td>Get next item in linked list of objects:</td>
<td>
<p class="code">class list
<p class="code">class list
public
public
  variable next object list
  variable next object list
  ...
  ...
end class
end class
%item object list
%item object list
...
...
Line 76: Line 78:
</p>
</p>
</td></tr>
</td></tr>
</table>
</table>
   
   
Line 92: Line 93:
<dd>In this case, as the right side of the assignment is evaluated,
<dd>In this case, as the right side of the assignment is evaluated,
each instance of the <code>@</code> phrase will invoke
each instance of the <code>@</code> phrase will invoke
the "get" method of the property is invoked to obtain its value - and to
the "get" method of the property to obtain its value &mdash; and to
perform any other actions done by the "get" method
perform any other actions of the "get" method.
(note that the property may not be a write-only property).
'''Note:''' the property may not be a write-only property.
</dl>
</dl>
   
   
Line 103: Line 104:
Consider the following two example assignment statements:
Consider the following two example assignment statements:
<p class="code">%x(10+%y) = @ + 17
<p class="code">%x(10+%y) = @ + 17
 
%nLis(%i):SelectSingleNode('customer[@id="' With %cusId With '"]'):Value = -
%nLis(%i):selectSingleNode('customer[@id="' with %cusId with '"]'):value = -
   %prefix With @ With %suffix
   %prefix with @ with %suffix
</p>
</p>
<dl>
<dl>
Line 117: Line 118:
<li>Item <code>%i</code> is obtained from the <var>XmlNodelist</var> <code>%nLis</code>.
<li>Item <code>%i</code> is obtained from the <var>XmlNodelist</var> <code>%nLis</code>.
<li>The resulting <var>XmlNode</var> is used as the context node to select
<li>The resulting <var>XmlNode</var> is used as the context node to select
the first <var>XmlDoc</var> node matching the XPath expression which is the
the first <var>XmlDoc</var> node matching the XPath expression (which is the
concatenation of '<code>customer[@id="</code>' with the value of <code>%cusId</code> with
concatenation of '<code>customer[@id="</code>' with the value of <code>%cusId</code> with
'<code>"]</code>'.
'<code>"]</code>').
</ul>
</ul>
Those operations are performed once, resuling in an <var>XmlNode</var>.  The
Those operations are performed once, resuling in an <var>XmlNode</var>.  The
<var>Value</var> property of that node is replaced with
<var>Value</var> property of that node is replaced with
the string which is the concatenation the value of <code>%prefix</code> with the
the string which is the concatenation the value of <code>%prefix</code> with the
former value of <var>Value</var> property with the value of <code>%suffix</code>.
former value of the <var>Value</var> property with the value of <code>%suffix</code>.
</dl>
</dl>
   
   
The above examples which use "@", where the left-hand
The above examples that use "@", where the left-hand
sub-expressions are evaluated only once, is more efficient than
sub-expressions are evaluated only once, are more efficient than
the equivalent
the equivalent
statements where the left-hand side is literally substituted for each
statements where the left-hand side is literally substituted for each
Line 136: Line 137:
However, in cases in which sub-expressions in the
However, in cases in which sub-expressions in the
left-hand side invoke some functions or properties
left-hand side invoke some functions or properties
which have side-effects, it is important to note that since the sub-expressions
that have side-effects, it is important to note that since the sub-expressions
are only evaluated once, the result of such an assignment can be different from
are only evaluated once, the result of such an assignment can be different from
a
a statement in which the left-hand side is literally substituted for each
statement in which the left-hand side is literally substituted for each
instance of <code>@</code> on the right-hand side.  Also, there may be sub-expressions
instance of <code>@</code> on the right-hand side.  Also, there may be sub-expressions
on the right-hand side which have side-effects, which could change the result of
on the right-hand side that have side-effects, which could change the result of
the sub-expressions of the left-hand side, were it substituted for <code>@</code>
the sub-expressions of the left-hand side, were they substituted for <code>@</code>
phrases.
phrases.
   
   
Programming practices which produce such side-effects are best avoided, but here
Programming practices that produce such side-effects are best avoided, but here
is an example:
is an example:
<p class="code">local function (string):getStateCode string len 2
<p class="code">local function (string):getStateCode string len 2
%bal float
%bal is float
%secretAccount Longstring Common
%secretAccount is longstring common
for 1 record where AcctNum = %this
for 1 record where AcctNum = %this
   for 1 record where AcctNum = %secretAccount
   for 1 record where AcctNum = %secretAccount
       %bal = Balance + 100
       %bal = Balance + 100
       change Balance To %bal
       change Balance to %bal
   end for
   end for
   %bal = Balance - 100
   %bal = Balance - 100
   change Balance To %bal
   change Balance to %bal
   return StateCode
   return StateCode
end for
end for
end function
end function
...
...
%balancesByState namedArraylist of float auto new
%balancesByState is namedArraylist of float auto new
%balancesByState:useDefault = true
%balancesByState:useDefault = true
fr where ...
fr where ...
Line 169: Line 169:
end for
end for
</p>
</p>
 
=="@" is not a "self-delimiting" phrase except before/after colon==
=="@" is not a "self-delimiting" phrase except before or after a colon==
   
   
In a <var class="product">User Language</var> expression, some characters
In a <var class="product">User Language</var> expression, some characters
or character sequences
or character sequences
are "self-delimiting", that is, they may be adjacent to other "words"
are "self-delimiting," that is, they may be adjacent to other "words"
in the expression without intervening whitespace. For example:
in the expression without intervening whitespace. For example:
<p class="code">%x=%x*%y+%x/(%z-1)</p>
<p class="code">%x=%x*%y+%x/(%z-1)</p>
In the expression on the right side of the above statement, no whitespace
In the expression on the right side of the above statement, no whitespace
Line 192: Line 192:
character or character sequence or
character or character sequence or
whitespace is required to separate words in an expression.
whitespace is required to separate words in an expression.
For example, the following assigns to <code>%c</code>
For example, the following statement assigns to <code>%c</code>
the concatenation of the string values of variables <code>%a</code>
the concatenation of the string values of variables <code>%a</code>
and <code>%b</code>:
and <code>%b</code>:
Line 202: Line 202:
colon) self-delimiting;
colon) self-delimiting;
for example, the following assigns to <code>%c</code>
for example, the following assigns to <code>%c</code>
the concatenation of the string values variables <code>%c</code>
the concatenation of the string values of variables <code>%c</code>
and <code>%b</code>:
and <code>%b</code>:
<p class=code>%c=@ with %b</p>
<p class=code>%c=@ with %b</p>
Line 211: Line 211:
</p>
</p>
   
   
==="@" may appear immediately before and/or after colon===
==="@" may appear immediately before or after a colon===
   
   
The colon character (<code>:</code>) behaves, in most contexts, as
The colon character (<tt>:</tt>) behaves, in most contexts, as
if it were a self-delimiting character, hence, no whitespace is
if it were a self-delimiting character, hence, no whitespace is
required between <code>:</code> and <code>@</code>.  For example:
required between <code>:</code> and <code>@</code>.  For example:
Line 219: Line 219:
The above statement, in effect, removes the first 9 bytes of
The above statement, in effect, removes the first 9 bytes of
the variable <code>%c</code>.
the variable <code>%c</code>.
 
=="@" and the SetText statement==
=="@" and the SetText statement==
   
   

Latest revision as of 20:46, 10 September 2013

This feature is new in version 7.9 of the Sirius Mods.

Some programming languages have operators that allow the right-hand side of an assignment statement to reference the left-hand side. In C, for example, i += 2 is equivalent to i = i + 2.

A feature introduced in version 7.9 of the Sirius Mods allows a much more generalized approach to doing this. In the right-hand side expression of an assignment statement, the "at" sign (@) character refers to the left-hand side of the assignment. For example:

%x = 4 %x = @:toPower(@) print %x

This fragment produces 256, that is, the same result that would be obtained by substituting '%x', the left side of the assignment, for '@' in '%x = @:ToPower(@)'. This is the value of %x raised to that same power.

The @ character is used in an expression on the right side of an assignment statement wherever a "value" phrase may occur, for example, a variable, literal, method expression, $function, or sub-expression. The @ portion of the expression is called the "@ phrase."

Examples

Some examples of the @ phrase are:

Add 2 to a Float variable:

%x = @ + 2

Raise a Float variable to its own power:

%x = @:toPower(@)

Code with long variable names can become much more readable:

%numberOfCustomerInteractions = @ + 1 %totalTimeUsedForTransaction = @ + %time

As opposed to:

%numberOfCustomerInteractions = %numberOfCustomerInteractions + 1 %totalTimeUsedForTransaction = %totalTimeUsedForTransaction + %time

Concatenate to string:

%foo = @ with '.suffix'

Concatenate to lowercased string:

%foo = 'prefix.' with @:toLower

Get number plus next prime number:

%foo = @ + @:nextPrime

Map variable using NamedArraylist:

%foo = %namedArrlis(@)

Get next item in linked list of objects:

class list public variable next object list ... end class %item object list ... %item = @:next

"@" may refer to variable, array/screen/image item, or property

When the @ phrase is in the right hand side of an assignment, the left side of the assignment is something which could be used as an expression. In terms of the operation (or semantics) of the @ phrase, there are two cases for the left side of the assignment:

a variable, array item, screen item, or image item
In this case, when the @ phrase is used on the right side, it simply refers to the value of the left side.
a property
In this case, as the right side of the assignment is evaluated, each instance of the @ phrase will invoke the "get" method of the property to obtain its value — and to perform any other actions of the "get" method. Note: the property may not be a write-only property.

Left-hand side sub-expressions evaluate once, prior to right-hand side

If the left side of an assignment statement contains sub-expressions, these are only evaluated once, prior to evaluating any of the right-hand side. Consider the following two example assignment statements:

%x(10+%y) = @ + 17 %nLis(%i):selectSingleNode('customer[@id="' with %cusId with '"]'):value = - %prefix with @ with %suffix

In the first assignment above:
The value 17 is added to the array element (or Arraylist item) located at position 10+%y. The expression 10+%y is only evaluated once.
In the second assignment above:
  • Item %i is obtained from the XmlNodelist %nLis.
  • The resulting XmlNode is used as the context node to select the first XmlDoc node matching the XPath expression (which is the concatenation of 'customer[@id="' with the value of %cusId with '"]').

Those operations are performed once, resuling in an XmlNode. The Value property of that node is replaced with the string which is the concatenation the value of %prefix with the former value of the Value property with the value of %suffix.

The above examples that use "@", where the left-hand sub-expressions are evaluated only once, are more efficient than the equivalent statements where the left-hand side is literally substituted for each instance of @ on the right-hand side, and so those sub-expressions are evaluated multiple times.

However, in cases in which sub-expressions in the left-hand side invoke some functions or properties that have side-effects, it is important to note that since the sub-expressions are only evaluated once, the result of such an assignment can be different from a statement in which the left-hand side is literally substituted for each instance of @ on the right-hand side. Also, there may be sub-expressions on the right-hand side that have side-effects, which could change the result of the sub-expressions of the left-hand side, were they substituted for @ phrases.

Programming practices that produce such side-effects are best avoided, but here is an example:

local function (string):getStateCode string len 2 %bal is float %secretAccount is longstring common for 1 record where AcctNum = %this for 1 record where AcctNum = %secretAccount %bal = Balance + 100 change Balance to %bal end for %bal = Balance - 100 change Balance to %bal return StateCode end for end function ... %balancesByState is namedArraylist of float auto new %balancesByState:useDefault = true fr where ... * Notice that getStateCode is only evaluated once, so a dollar * is only stolen once for each account processed: %balancesByState(AcctNum:getStateCode) = @ + Balance end for

"@" is not a "self-delimiting" phrase except before or after a colon

In a User Language expression, some characters or character sequences are "self-delimiting," that is, they may be adjacent to other "words" in the expression without intervening whitespace. For example:

%x=%x*%y+%x/(%z-1)

In the expression on the right side of the above statement, no whitespace is required around any of the following characters:

* + / ( - )

Other characters, for example a comma, or character sequences, for example <=, also do not require surrounding whitespace when used in an expression. However, for non-self-delimiting phrases, either a self-delimiting character or character sequence or whitespace is required to separate words in an expression. For example, the following statement assigns to %c the concatenation of the string values of variables %a and %b:

%c=%a with %b

The following, however, is a syntax error, because the concatenation operator (with) is not self-delimiting:

%c=%a with%b

Similarly, the @ phrase is not (except when preceding a colon) self-delimiting; for example, the following assigns to %c the concatenation of the string values of variables %c and %b:

%c=@ with %b

Each of the following, however, is a syntax error, because neither with nor @ is self-delimiting:

%c=@with %b %c=%b with@

"@" may appear immediately before or after a colon

The colon character (:) behaves, in most contexts, as if it were a self-delimiting character, hence, no whitespace is required between : and @. For example:

%c = @:substring(10)

The above statement, in effect, removes the first 9 bytes of the variable %c.

"@" and the SetText statement

In the SetText statement, the @ character can occur in the right hand side, and, just as with the ordinary assignment statement, @ refers to the value of the left hand side of the statement. For example:

%s = 'Hello, world!' SetText %s = {@} and its reverse {%s:Reverse} Print %s

The result of the above fragment is Hello, world! and its reverse !dlrow ,olleH.

Authorizing products

The @ phrase is available to any customer who owns any of the following products:

  • Fast/Unload User Language Interface
  • Janus SOAP
  • Janus Sockets
  • Janus Web Server
  • Sirius Functions

The Html block is also available if the Limited Janus Web Server capability is available.