04-12-2018 12:52 PM
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?
04-12-2018 2:58 PM
The documentation is correct, because it says "statically". Try to address it dynamically and you'll see (it is ABAP, sigh).
04-12-2018 2:58 PM
The documentation is correct, because it says "statically". Try to address it dynamically and you'll see (it is ABAP, sigh).
04-12-2018 4:04 PM
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 🙂
04-12-2018 4:46 PM
Well, if you read the full documentation, you find
When reusing helper fields in different expressions, the following applies:
And also
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 ...
04-12-2018 6:28 PM
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.
04-12-2018 9:38 PM
But I'm glad you asked the question. Hopefully I'll remember what to do if I encounter it!