Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert
27,234


This one is a tribute to the community, to SCN, to you. One of you, volker.wegert2; has blogged his ABAP Wishlist - IS INSTANCE OF some years ago and reminded us again and again. Others backed him and I myself participated a little bit by forwarding the wish to the kernel developers. And constant dripping wears the stone, ABAP 7.50 comes with a new relational expression IS INSTANCE OF, even literally.

 

If you wanted to find out whether a reference variable of a given static type can point to an object before ABAP 7.50, you had to TRY a casting operation that might look like something as follows:
    DATA(typedescr) = cl_abap_typedescr=>describe_by_data( param ).

DATA:
elemdescr TYPE REF TO cl_abap_elemdescr,
structdescr TYPE REF TO cl_abap_structdescr,
tabledescr TYPE REF TO cl_abap_tabledescr.
TRY.
elemdescr ?= typedescr.
...
CATCH cx_sy_move_cast_error.
TRY.
structdescr ?= typedescr.
...
CATCH cx_sy_move_cast_error.
TRY.

tabledescr ?= typedescr.
...
CATCH cx_sy_move_cast_error.
...
ENDTRY.
ENDTRY.
ENDTRY.


In this example we try to find the resulting type of an RTTI-operation.

 

With ABAP 7.50 you can do the same as follows:

 
    DATA(typedescr) = cl_abap_typedescr=>describe_by_data( param ).

IF typedescr IS INSTANCE OF cl_abap_elemdescr.
DATA(elemdescr) = CAST cl_abap_elemdescr( typedescr ).
...
ELSEIF typedescr IS INSTANCE OF cl_abap_structdescr.
DATA(structdescr) = CAST cl_abap_structdescr( typedescr ).
...
ELSEIF typedescr IS INSTANCE OF cl_abap_tabledescr.
DATA(tabledescr) = CAST cl_abap_tabledescr( typedescr ).
...
ELSE.
...
ENDIF.





The new predicate expression IS INSTANCE OF checks, if the dynamic type of the LHS operand is more special or equal to an RHS type. In fact it checks whether the operand can be down casted with that type. In the above example, such a casting takes place after IF, but its your decision if you need it. If you need it, there is even a shorter way to write it. A new variant of the CASE -WHEN construct:!

 
    DATA(typedescr) = cl_abap_typedescr=>describe_by_data( param ).

CASE TYPE OF typedescr.
WHEN TYPE cl_abap_elemdescr INTO DATA(elemdescr).
...
WHEN TYPE cl_abap_structdescr INTO DATA(structdescr).
...
WHEN TYPE cl_abap_tabledescr INTO DATA(tabledescr).
...
WHEN OTHERS.
...
ENDCASE.

The new TYPE OF and TYPE additions to CASE and WHEN allow you to write IS INSTANCE OF as a case control structure. The optional INTO addition does the casting for you, I think that's rather cool.

 

B.T.W., the new IS INSTANCE OF and CASE TYPE OF even work for initial reference variables. Then they check if an up cast is possible.This can be helpful for checking the static types of formal parameters or field symbols that are typed generically. Therefore IS INSTANCE OF is not only instance of but might also be labeled as a type inspection operator.

 

For more information see:

 

35 Comments
Labels in this area