天天看點

DEFAULT keyword in ABAP and Optional Class in Java

It is ok to use this new ABAP syntax about table expression here since object_ext is passed from UI framework and it is rarely possible that there would be partner or customer to manually pass any other value to parameter it_data.

DEFAULT keyword in ABAP and Optional Class in Java

According to ABAP help, if no row by specified key could be found, exception will be raised, instead of sy-subrc set to a non-zero value for READ TABLE XXX.

DEFAULT keyword in ABAP and Optional Class in Java

so ABAP provides another key word DEFAULT which allows you to provide a default value if specified row does not exist to avoid exception. In example below, line 17 will raise exception while line 19 not.

DEFAULT keyword in ABAP and Optional Class in Java

In Java 8 there is similar mechanism: new class Optional is introduced to avoid application developers’ endless of If XXX != null code to check null:

DEFAULT keyword in ABAP and Optional Class in Java

Unlike many other annotation, this Utility class is not implemented in JVM layer, but just in language layer, as a result it is very easy to develop an Optional class in ABAP as well.

DEFAULT keyword in ABAP and Optional Class in Java