Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
SafaBahoosh
Product and Topic Expert
Product and Topic Expert
6,624
From release 7.57, the new addition ELSE UNASSIGN can be specified in various cases of the statement ASSIGN, like dynamic assignments/access, assignments of dynamic components or table expressions.

An assignment passes the content of a source to a target data object. The general syntax is

ASSIGN  mem_area  TO  <fs>  [ELSE UNASSIGN].

Which assigns the memory area specified using mem_area to the field symbol <fs>. The reference in a field symbol is set using an assignment of a memory area to the field symbol by the statement “ASSIGN” and is deleted by the statement “UNASSIGN” which initializes the field symbol <fs>. The newly defined addition ELSE UNASSIGN to the statement ASSIGN, unassigns the field symbol if the assignment is not successful, meaning, in case sy-subrc is not initial.

The field symbol “points to” the contents of the field mem_area at runtime, i.e. every change to the contents of mem_area is reflected in and vice versa. If the field symbol is not typed (see FIELD-SYMBOL ), the field symbol adopts the type and atrributes of the field mem_area at runtime, particularly the conversion exit. Otherwise, when the assignment is made, the system checks whether the type of the field mem_area matches the type of the field symbol. After a successful assignment, the field symbol refers to the assigned memory area and can be used in operand positions.

The memory area mem_area can be specified statically, in which a statically known data object or part of such an object is assigned, or dynamically, which is used to dynamically access data objects  especially for dynamic access to attributes of classes and interfaces or components of structures. In such variants, the statement ASSIGN sets the return code "sy-subrc". If the assignment is successful, sy-subrc is set to 0, otherwise to 4. In these variants, no exception occurs in case of an unsuccessful assignment.

Besides, the variant for specifying the memory area of the statement ASSIGN can also assigns the result of the table expression table_exp or table expression chaining to the field symbol. The result of a table expression in these positions is always a temporary field symbol. In this variant, the statement ASSIGN sets the return code sy-subrc. If the specified line is found, sy-subrc is set to 0. If the assignment is not successful, sy-subrc is set to 4, except when the end of the table is reached in binary searches in sorted tables. In this case, sy-subrc is set to 8.

The variants differ as to how the system behaves after an unsuccessful assignment. In such case, the behaviour depends on the assigned mem_area and on the addition ELSE UNASSIGN:

  • If a static assignment is not successful, sy-subrc is not set. No memory area is assigned to the field symbol. The field symbol has the state unassigned after the ASSIGN statement. The addition ELSE UNASSIGN is used implicitly and must not be specified.

  • If a dynamic assignment, an assignment of dynamic components, a dynamic access or an assignment of a table expression is not successful, sy-subrc is set to 4 or 8 and:

    • If ELSE UNASSIGN is not specified, the field symbol keeps its previous state.

    • If ELSE UNASSIGN is specified, no memory area is assigned to the field symbol. The field symbol has the state unassigned after the ASSIGN statement.




Some invalid dynamic specifications in the assignment of dynamic components do not set sy-subrc but raise an exception.

  • An assignment of the constructor operators NEW or CASE is either successful or leads to an exception and the addition ELSE UNASSIGN must not be used.


If an assignment would lead to illegal memory accesses, an exception is raised for both static and dynamic ASSIGN statements.

Example


After a successful assignment, the next assignment is not successful because of a wrong dynamic specification.
FINAL(field) = `exists`.

ASSIGN ('field') TO FIELD-SYMBOL(<fs>) ELSE UNASSIGN.
ASSERT sy-subrc = 0 AND <fs> IS ASSIGNED.

ASSIGN ('exists') TO <fs> ELSE UNASSIGN.

ASSERT sy-subrc = 4 AND <fs> IS NOT ASSIGNED.

IF <fs> IS NOT ASSIGNED.
cl_demo_output=>display( 'The field symbol is unassigned' ).
ENDIF.

here sy-subrc is set to 4 and the field symbol that was assigned before is unassigned.

it is more clear in the debugging mode:


As the field symbol is successfully unassigned by 'ELSE UNASSIGN' command, we get the pop up as expected:


if you omit this command from line 14, although the sy-subrc is 4, the field symbol still contains the amount from the previous step:


and the runtime error occurs.


So, the addition 'ELSE UNASSIGN' guarantees the correct assignment of the field symbol in the program.

For more information regarding assignment in ABAP, check ABAP Keyword Documentation and the links provided in the text.
8 Comments
Sandra_Rossi
Active Contributor
0 Kudos
Thanks. Isn't there an error at this line? I guess NOT should be removed for your demonstration:
ASSERT sy-subrc = 4 AND <fs> IS NOT ASSIGNED.

What is the rationale of introducing ELSE UNASSIGN? I always used IF sy-subrc = 0 after ASSIGN, I thought it was sufficient. I mean, I don't see a big interest in ELSE UNASSIGN.

I guess there could be lots of other same-kind improvements for basic statements, like is it planned to have something like SQL COALESCE but for IS NOT INITIAL? I often use this kind of statement:
a = COND #( WHEN a IS NOT INITIAL THEN a ELSE anything ).

which could be shortened:
a = COALESCE a anything.
SafaBahoosh
Product and Topic Expert
Product and Topic Expert
Hello Sandra,

if you debug you get it better


it says, in case sy-subrc is not equal to 4 and <fs> is not empty ( as we expected) then do the assert.

if you take the "Else Unassign" out, you see the problem:


although the assignment is not correct, the field symbol is still full. so we need the addition to empty the field symbol in case wrong assignment happens. i update the blog to make it more clear 🙂

Regards

Safa

 
Sandra_Rossi
Active Contributor
0 Kudos
Thanks I understand. Oh now we have to distinguish a different behavior due to ELSE UNASSIGN. I feel it will bring even more confusion. I'm not fan of it. I would have preferred words like ELSE INITIAL. Well, we'll see.

NB: we could also implement the same for REF #( ... OPTIONAL ) but probably people use it less frequently than ASSIGN --> REF #( ... ELSE INITIAL ).
shais
Participant

Unless I'm missing something, I don't see any different behaviour.

It seems like a shortcut (implemented in the SAP kernel and more efficient, I guess) for:

ASSIGN ('exists') TO <fs>.
IF SY-SUBRC <> 0.
UNASSIGN <fs>.
ENDIF.
matt
Active Contributor
0 Kudos
Just syntactic sugar.
matt
Active Contributor
To me, the reuse of a field-symbol is itself a code smell. I understand the example, but I doubt very much I would use it in a real development.
shais
Participant
0 Kudos
The common (not that smelly) use case is a dynamic loop.
matt
Active Contributor
I'd put the assignment and subsequent handling in a method in the loop. Which is probably why I've never had an issue with this. 🙂
Labels in this area