[英]Preferred Boolean type for ABAP Development
SAP doesn't have a core Data Type for boolean values. SAP 没有用于布尔值的核心数据类型。 Additionally, higher level boolean types in SAP typically have three states: true ('X'), false (' ') and unknown ('-').
此外,SAP 中的高级布尔类型通常具有三种状态:真 ('X')、假 (' ') 和未知 ('-')。
Now obviously booleans are a cornerstone of a lot of my development work but I realised that I haven't been terribly consistent in my type (data element) usage.现在显然布尔值是我很多开发工作的基石,但我意识到我在我的类型(数据元素)使用方面并没有非常一致。 So far I believe these are the most common:
到目前为止,我认为这些是最常见的:
abap_bool
: defined in the abap type-pool, unconstrained but constants are defined for true, false, unknown, yes and no abap_bool
: 在abap_bool
类型池中定义,无约束但常量定义为 true、false、unknown、yes 和 noos_boolean
: data element, Yes ('X') or No (' ') os_boolean
: 数据元素,是 ('X') 或否 (' ')xfeld
: data element, True ('X') or False (' '), lacks a field label, described as a checkbox xfeld
: 数据元素,True ('X') 或 False (' '),缺少字段标签,描述为复选框In my code I've mainly used abap_bool
as I can then work with constants instead of character values, not that I couldn't assign abap_true
to an xfeld
.在我的代码中,我主要使用
abap_bool
因为我可以使用常量而不是字符值,并不是说我不能将abap_true
分配给xfeld
。 However, I've been cautioned that this type pool might not always be available.但是,有人提醒我,这种类型的池可能并不总是可用。
I'm now wondering about the best practices for boolean values, specifically:我现在想知道布尔值的最佳实践,特别是:
I use the type pool ABAP
and its constants in coding.我在编码中使用类型池
ABAP
及其常量。 It should always be available, though you may have to include it manually on older systems.它应该始终可用,尽管您可能必须在较旧的系统上手动包含它。 For dictionary elements, I prefer to create my own data elements using any of the default domains so that I can add descriptions to suit my needs.
对于字典元素,我更喜欢使用任何默认域创建我自己的数据元素,以便我可以添加描述以满足我的需要。 You can use WDY_BOOLEAN as well.
您也可以使用 WDY_BOOLEAN。
According to rule 6.11 of the Official ABAP Programming Guidelines , you should use abap_bool.根据官方 ABAP 编程指南的规则 6.11,您应该使用 abap_bool。
Rule 6.11: Use the abap_bool Data Type for Truth Values
规则 6.11:对真值使用 abap_bool 数据类型
To explicitly handle truth values, use the abap_bool type as a workaround for a real Boolean data type.
要显式处理真值,请使用 abap_bool 类型作为真实布尔数据类型的变通方法。 A data object that is declared in this way is not supposed to contain other values than the corresponding constants, abap_true and abap_false (as well as abap_undefined).
以这种方式声明的数据对象不应包含除相应常量 abap_true 和 abap_false(以及 abap_undefined)之外的其他值。
Update for Release 7.40, SP08 : 7.40 版 SP08 的更新:
With Release 7.40, SP08 you get the Predicative Method Calls and you can code something like:在 7.40 版 SP08 中,您可以获得预测方法调用,您可以编写如下代码:
IF cl_abap_demo_services=>is_production_system( ). "There is no '= abap_true' needed!
cl_demo_output=>display(
'This demo cannot be executed in a production system' ).
LEAVE PROGRAM.
ENDIF.
Your method (in the example is_production_system
) must return a ABAP_BOOL-value (abap_true ('X') or abap_false (' '))您的方法(在示例
is_production_system
)必须返回ABAP_BOOL 值(abap_true ('X') 或 abap_false (' '))
The online help has an example. 在线帮助有一个例子。
Sadly, this is the bane of ABAP... not having a fundamental boolean type... only boolean expressions.可悲的是,这是 ABAP 的祸根……没有基本的布尔类型……只有布尔表达式。 So the wonderful thing in ABAP is that there are so many boolean types to choose from!
所以 ABAP 的美妙之处在于有如此多的布尔类型可供选择!
After many years I believe the best way is to just roll your own (sadly).多年后,我相信最好的方法是自己动手(可悲的是)。 If you do class-based development, then always just add a
true
and false
(and undefined
if you wish) constants in your base class, and define your own boolean and/or boolean_undefined types.如果你这样做基于类的发展,那么永远只是增加一个
true
和false
(和undefined
在你的基类,如果你愿意的话)的常量和定义自己的布尔和/或boolean_undefined类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.