gpt4 book ai didi

for-loop - FOR 循环中不兼容的 Field-Symbol 类型

转载 作者:行者123 更新时间:2023-12-04 12:31:40 25 4
gpt4 key购买 nike

请问有什么解决办法吗

TYPES: BEGIN OF ty_table1,
field1 TYPE string,
END OF ty_table1,
tt_table1 type STANDARD TABLE OF ty_table1 with EMPTY KEY,
BEGIN OF ty_table2,
field1 TYPE string,
END OF ty_table2,
tt_table2 type STANDARD TABLE OF ty_table2 with EMPTY KEY.
DATA: lt_table TYPE table OF ty_table1,
lt_final TYPE table OF ty_table2.

DATA(lt_table1) = VALUE tt_table1( FOR <fs_struct> IN lt_table WHERE ( field1 EQ 'TEST' )
( CORRESPONDING #( <fs_struct> ) ) ).

DATA(lt_table2) = VALUE tt_table2( FOR <fs_struct> IN lt_final WHERE ( field1 EQ 'TEST1' )
( CORRESPONDING #( <fs_struct> ) ) ).

在这里,我们将在第二个构造函数表达式中遇到语法错误:

"<FS_STRUCT>" was already declared with the type "TY_TABLE1" and cannot be used with the type "TY_TABLE2" here.

有没有办法“取消分配”<FS_STRUCT>在第一个声明之后?因此,它可以在下一个即将发布的针对不同表的 FOR 语句中使用。

正常UNASSIGN语句不能用于取消分配 <fs_struct>因为它在 FOR 循环之外没有作用域。

最佳答案

FOR 语句的目标字段 has the same semantics作为LET expressions :

A helper field specified in a LET expression is valid in the context in which the LET expression is specified. This can be a full expression or just part of an expression. All helper fields of a full expression are in the same namespace. A previously specified helper field cannot be specified in a further LET expression of the same expression. Furthermore, the helper fields are in the same namespace as the data objects or field symbols of the current procedure or program. Helper fields cannot be defined in a LET expression if a data object or field symbol with the same name already exists in the procedure or program of the expression. Conversely, no data objects or field symbols with names assigned to helper fields can be declared after an expression with a LET expression.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 of 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 of a different expression and the derived data type does not match, the helper field cannot be used there and a syntax error occurs.

因此实际上您的代码中有两个不同的声明(理论上可以包含不同的类型)。然而,ABAP 语言强制类型匹配,可能是为了避免具有相同名称的变量突然持有不同类型的混淆情况(这也可能是防止运行时冲突所必需的)。

也没有办法影响用 LET² 声明的局部变量的推断类型。

据我所知,答案显然是否定的,您真的应该为不同的类型使用不同的名称。此外,在通用字段符号上使用 CORRESPONDING 对我来说听起来很危险,我会避免这种情况。


²除非您更改派生类型的值,例如。通过 CONV将一个表转换为另一个表(尽管这可能不适用于您的情况):

DATA(second_result) = VALUE second_table_type( 
FOR <entry> IN CONV first_table_type( second_source )
WHERE ( field1 EQ 'TEST1' )
( CORRESPONDING #( <entry> ) )
).

关于for-loop - FOR 循环中不兼容的 Field-Symbol 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68618709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com