cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Variable preceded with exclamation mark

Former Member
4,791

A colleague of mine just noticed something that I have never seen before and can't see the purpose of - preceding a variable with !.


DATA:
  mydate TYPE d.

mydate = sy-datum.
ADD 1 TO !mydate.

WRITE: mydate, !mydate.

The memory reference is identical, changing MYDATE changes !MYDATE too. Works for internal tables too.

Odd.

Scott

Accepted Solutions (1)

Accepted Solutions (1)

retired_member
Product and Topic Expert
Product and Topic Expert

! is an escape symbol.

You can write ! directly in front of a operand name to distinguish it from the same ABAP word. Each word (except the first one) of an ABAP statement that is preceded by ! is treated as an operand. The escape symbol itself is not part of the name and ignored during execution.

The use of ! is normally not required. The ABAP compiler is smart enough to distinguish between ABAP words and operands as in:

<b>DATA data TYPE type.</b>

But there are rare cases ...

<b>FORM test USING !changing CHANGING !using.</b>

The first ! is required or you get a syntax error.

PS: I found out about that only some weeks ago and now its part of the ABAP documentation

Former Member
0 Likes

Thanks Horst.

I'm sure there must be some deep cosmological reasoning governing the learning of information and the near-after need for that information. Similar things seem to happen a lot for me.

It's nice to have you in the forum.

Scott

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes

Cosmological reasoning is even stronger than you can imagine.

Believe it or not.

Exactly in the moment, you were posting the question, I was discussing with my boss about that subject (naming conventions, reserved words, need of ! etc.). When he left my office, I turned to the screen and found your entry.

ChristianFi
Active Participant
0 Likes

Horst,

thanks for the information, I was puzzeled once too, but thought it would be an odd programming style.

While I understand the purpose I do not see the requirement. What could be the reason that I would want to name my variable like a key word?

I mean the maintenance of a program (especially if written by someone else) is not only a question of abap knowledge but requires also to get used to a certain style.

Maybe the compiler is smart enough - I am not.

Having established some naming conventions on a rather deep level (types, variables) I would be curious if there is a situation where you do not have a choice but to use that escape symbol.

Christian

retired_member
Product and Topic Expert
Product and Topic Expert

Hi Christian,

from my point of view, you are perfectly right.

One should try not to use "reserved ABAP words" at all(even if ABAP doesn't have "reserved words" in a technical sense9. And to be perfectly sure to do so, one must follow some naming conventions because you can never be sure that a unprefixed word, that is not an ABAP word today, will not become an ABAP word in a future release.

But this is my personal opinion. Officialy, ABAP does not enforce naming conventions and names that are equal to ABAP words are allowed. In some cases like

DATA i TYPE i.

this can be okay (different namespaces for types and data objects). But other declarations that lead to syntactically correct statements like

READ TABLE TABLE WITH TABLE KEY TABLE = TABLE.

should of course be avoided, even if you do not have to use the escape symbol!

So, it is the developers own responsibility to keep his or her source code readable. Naming conventions can be a great help, but are no necessity. But in fact there should never be a situation, where you use names that force you to use the escape symbol (introducing ! into above READ TABLE statment is not mandatory but would make it more readable).

Regards

Horst

Answers (0)