Application Development and Automation 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: 
Read only

Could not used the variable in the inline declaration

FredericGirod
Active Contributor
3,317

Hi

I am trying to do a simple code like this one

loop at my_lines 
     reference into data(o_line).
  data(my_summ) = o_line->my_quantity + my_sum. 
endloop.

(the objective is not to do a simple sum, this is just an example)

but, I have an error "my_sum is unknown"

is it a real limitation of the inline declaration ?

1 ACCEPTED SOLUTION
Read only

maheshpalavalli
Active Contributor
2,593

It looks like it is not possible, check the comment from horst.keller

https://blogs.sap.com/2013/05/23/abap-news-for-release-740-inline-declarations/#comment-349626

I couldn't understand the standard help terminology but I think they are talking about the same.

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abendata_inline.htm

Just like the statement DATA, an inline declaration does not open a local context for the current statement block. An inline declaration for a variable can only be made once within a context and the variable cannot yet be declared there using DATA.

is this the same? that statement is super confusing for me 😄

Thanks,
Mahesh

Hi

I am trying to do a simple code like this one

loop at my_lines 
     reference into data(o_line).
  data(my_summ) = o_line->my_quantity + my_sum. 
endloop.

(the objective is not to do a simple sum, this is just an example)

but, I have an error "my_sum is unknown"

is it a real limitation of the inline declaration ?

7 REPLIES 7
Read only

maheshpalavalli
Active Contributor
2,594

It looks like it is not possible, check the comment from horst.keller

https://blogs.sap.com/2013/05/23/abap-news-for-release-740-inline-declarations/#comment-349626

I couldn't understand the standard help terminology but I think they are talking about the same.

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abendata_inline.htm

Just like the statement DATA, an inline declaration does not open a local context for the current statement block. An inline declaration for a variable can only be made once within a context and the variable cannot yet be declared there using DATA.

is this the same? that statement is super confusing for me 😄

Thanks,
Mahesh

Read only

2,593

I think you have right with this sentence

Just like the statement DATA, an inline declaration does not open a local context for the current statement 

even if when we used FOR ... IN ... the system do exactly the oposite

Read only

2,593

This sentence explains that DATA(xxx) is just like DATA, it's not a new notion of "local context for the current statement block" like in other programming languages (in a method the same variable can be different if declared in separate { ... } blocks), and consequently DATA(xxx) cannot be repeated in the same "context" (procedures, instances of classes, classes, and programs).

It's more this sentence:

The date type of the variable is determined by the operand type

The compiler tries to determine the type of "my_sum" based on the types of the Right-Hand Side, but there is a kind of recursive determination because "my_sum" also appears in the RHS.

Read only

2,593

Thanks Sandra Rossi & frdric.girod for the explanation, even in other languages like javascript also this is not possible, as it makes sense that the LHS type is dependent on the RHS type, but it will be based on the total RHS value type, in this case it will be

data(dyn_variable) = (some value) + (dyn_variable(with undefined type)) => so an error.

Read only

Sandra_Rossi
Active Contributor
2,593

Typo in your code (my_summ -> my_sum).

No other choice than doing it in two steps:

data(my_sum) = 0. 
loop at my_lines 
     reference into data(o_line).
  my_sum = o_line->my_quantity + my_sum. 
endloop.
Read only

FredericGirod
Active Contributor
0 Likes
2,593

sandra.rossi , yes I exactly do that. But ... I don't know why, but I did not like to use the DATA ... anymore. I try to avoid it as much as possible.

Read only

matt
Active Contributor
2,593

But you're not using DATA. You're using data(my_sum)=0.