ABAP Keyword Documentation → ABAP - Reference → Declarations → Local Declarations in Expressions
let_exp - LET ... IN
Other versions:
7.31 | 7.40 | 7.54
Syntax
... LET {var1 = rhs1}|{<fs1> = wrexpr1}
{var2 = rhs2}|{<fs2> = wrexpr2} ... IN ...
Alternatives
1. ... var = rhs ...
2. ... <fs> = wrexp ...
Effect
A LET
expression defines variables var1
,
var2, ... or field symbols <fs1>
, <fs2>
,
... as local auxiliary fields in an expression and assigned values to them. When declared, the auxiliary
fields can be used in the operand positions of the expression. There is no way of accessing an auxiliary
field statically outside its expression. The documentation of an expression specifies whether it contains
a LET
expression and in which positions. Any LET
expressions in an expression or subexpression are evaluated first.
An auxiliary field specified in a LET
expression is valid in the context
in which the LET
expression is specified. This can be a full expression or
just part of an expression. All auxiliary fields of a full expression are in the same namespace. A previously
specified auxiliary field can be specified in a further LET
expression of
the same expression. Furthermore, the auxiliary fields are in the same namespace as the data objects
or field symbols of the current procedure or program. Auxiliary fields cannot be defined in a
LET expression if a data object or field symbol with the same name already exists in the procedure
or program of the expression. Conversely, no data objects or field symbols with names given to auxiliary fields can be declared after an expression with a LET
expression.
When reusing auxiliary fields in different expressions, the following applies:
- If an auxiliary field is defined for the first time in the current procedure or program, it is declared inline.
-
If an auxiliary field in the current procedure or program is defined again in a
LET
expression in a different expression and the derived data type matches, the auxiliary field is bound to this expression and can be used there. -
If an auxiliary field in the current procedure or program is defined again in a
LET
expression in a different expression and the derived data type does not match, the auxiliary field cannot be used there and a syntax error occurs.
Notes
-
LET
expressions can currently only be used in constructor expressions. Subexpressions in whichLET
expressions can be used are iteration expressions withFOR
and results specified afterTHEN
andELSE
in the conditional expressionsCOND
andSWITCH
. -
In the assignment of a value operator
VALUE
to structures or internal tables, values from the left side can be saved to auxiliary fields using aLET
expression before the left side is overwritten. -
An auxiliary field defined in a
LET
expression can be addressed dynamically in the entire current context. This is not recommended, however, since expressions are not supposed to produce side effects. -
It is a good idea to use
LET
expressions whenever auxiliary fields are required for performance reasons (by avoiding multiple calculations) or the readability of an expression or whenever values from the left side of an assignment in aVALUE
operator are needed on the right side.
Example
See LET
expression.
Alternative 1
... var = rhs ...
Effect
Defines a local auxiliary variable var
as an auxiliary field in a LET
expression. The value of the right side, rhs, is assigned to the auxiliary variable as an initial value.
For the right side, rhs
, the same can be specified as in a regular assignment
using the assignment operator =
.
The data type of the auxiliary is determined from the right side, rhs
, in
exactly the same way as when an inline declaration
DATA(var) is specified on the left side of an assignment operator using the
assignment operator =
. It must be possible to determine the data type in full from the right side to avoid syntax errors.
Notes
-
The auxiliary variables declared in a
LET
expression generally retain the value defined byrhs
while the expression is being calculated. It is possible to modify the value of an auxiliary variable in the expression, for example by binding to aCHANGING
parameter of a method, but this would be unusual. -
The auxiliary variables declared in a
LET
expression are a good example of where long readable names are not necessary and can even harm readability. The auxiliary variables can only be used in their own expression, which means that short names (possibly even single-character names) are enough.
Example
Defines three local auxiliary variables, x
, y
,
and z
, in a constructor expression to construct the values of a structure. The values of the auxiliary variables are used for the structure components.
TYPES:
BEGIN OF struc,
col1 TYPE i,
col2 TYPE i,
END OF struc.
DATA(rnd) = cl_abap_random_int=>create(
seed = CONV i( sy-uzeit ) min = 1 max = 10 ).
DO 5 TIMES.
DATA(struc) = VALUE struc(
LET x = rnd->get_next( )
y = x * x
z = sy-index * 1000 IN col1 = x + z
col2 = y + z ).
cl_demo_output=>write( struc ).
ENDDO.
cl_demo_output=>display( ).
Alternative 2
... <fs> = wrexp ...
Effect
Defines a local field symbol <fs>
as an auxiliary field in a LET
expression. Here, the result of the
writable expression
wrexp
is assigned to the field symbol. The same applies here as in assignments
of writable expressions using ASSIGN
,
which means that only the expressions specified there can be used. The typing of the field symbol is
determined by the type of wrexp
and is performed in the same way as in inline
declarations of a field symbol using the statement FIELD-SYMBOL
.
Example
Defines a field symbol and a variable as auxiliary fields in a LET
expression in a conversion expression. The rows of an internal table are assigned to the field symbol.
TYPES:
BEGIN OF date,
year TYPE c LENGTH 4,
month TYPE c LENGTH 2,
day TYPE c LENGTH 2,
END OF date,
dates TYPE TABLE OF date WITH EMPTY KEY.
DATA(dates) = VALUE dates(
( year = '2013' month = '07' day = '16' )
( year = '2014' month = '08' day = '31' )
( year = '2015' month = '09' day = '07' ) ).
DO lines( dates ) TIMES.
DATA(isodate) = CONV string(
LET <date> = dates[ sy-index ]
sep = '-'
IN <date>-year && sep && <date>-month && sep && <date>-day ).
cl_demo_output=>write( isodate ).
ENDDO.
cl_demo_output=>display( ).