Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How local is a 'local auxiliary field'?

pokrakam
Active Contributor

A colleague noticed something interesting: If we use the same variable name in multiple FOR or LET expressions with different data types, we get type conflicts.

The documentation calls them "local auxiliary field", which I always interpreted as local to the expression - particularly since it also states "There is no way of accessing an auxiliary field statically outside its expression".

But:

TYPES numtab  TYPE STANDARD TABLE OF i WITH DEFAULT KEY.
TYPES chartab TYPE STANDARD TABLE OF c WITH DEFAULT KEY.

DATA(nums)  = VALUE numtab( ( 1 ) ( 2 ) ( 3 ) ).
DATA(nums2) = VALUE numtab( FOR x IN nums ( x ) ).

DATA(chars)  = VALUE chartab( ( 'A' ) ( 'B' ) ( 'C' ) ).
DATA(chars2) = VALUE chartab( FOR x IN chars ( x ) ).  "Error 1

"Just for kicks let's try the same with LET
DATA(nums3)  = VALUE numtab( LET a = 1 IN ( a ) ).
DATA(chars3) = VALUE chartab( LET a = 'A' IN ( a ) ).  "Error 2

x = 5.  "Error 3: X cannot be used here

Errors 1 and 2 are "X was already declared with the type "I" and cannot be used with the type "C" here.

Error 3 states that "X cannot be used here" which matches the docu.

So is the documentation vaguely worded or is this not as local as it is designed to be?

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert

The documentation is correct, because it says "statically". Try to address it dynamically and you'll see (it is ABAP, sigh).

5 REPLIES 5

horst_keller
Product and Topic Expert
Product and Topic Expert

The documentation is correct, because it says "statically". Try to address it dynamically and you'll see (it is ABAP, sigh).

0 Kudos

Thanks Horst, I thought as much.

I think the docu could be a little clearer, as "local helper fields in an expression" is misleading. Even the German "in einem Ausdruck als lokale Hilfsfelder" makes it sound like it's expression-local.

Well, semantically it is, but technically it isn't. Or is it the other way around? I think I'll go home now, it's making my head hurt 🙂

horst_keller
Product and Topic Expert
Product and Topic Expert

Well, if you read the full documentation, you find

When reusing helper fields in different expressions, the following applies:

  • If a helper field is defined for the first time in the current procedure or program, it is declared inline.
  • If a helper 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 helper field is bound to this expression and can be used there.
  • If a helper 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 helper field cannot be used there and a syntax error occurs.

And also

  • A helper 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.

In fact, that behavior is a kind of nuisance and I simply didn't want to say it as bluntly as "Hello readers, LET-variables are global variables that want to appear as local variables" 😉

It is the old ABAP flaw. No local variables beyond procedures. And expressions are a compiler and not a runtime game -> headache ...

My bad! Never argue with Horst Keller 🙂
In my defense, I carefully read the FOR docu as that's where we encountered the issue. It isn't mentioned there. LET was an afterthought, and you are right it is quite explicit in the LET docu.

I think the huge range of compatibility is worth putting up with some of ABAP's quirks.

matt
Active Contributor

But I'm glad you asked the question. Hopefully I'll remember what to do if I encounter it!