Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of ?= in ABAP operators?

Tags:

abap

My question is just as the title has, What's the difference between = and ?= in ABAP operators?

like image 339
abusemind Avatar asked Aug 03 '09 03:08

abusemind


Video Answer


1 Answers

?= is the (down)casting operator. It's used for assignments between reference variables, whose assignability is checked as early as the runtime starts.

C.f. general explanation at wikipedia.

Example:

DATA fruit TYPE REF TO zcl_fruit.
DATA apple TYPE REF TO zcl_apple. " inherits from zcl_fruit
DATA apricot TYPE REF TO zcl_apricot. " inherits from zcl_fruit

...

case fruit->type.
  when 'apple'.
    apple ?= fruit.
    seeds = apple->seeds.
  when 'apricot'.
    apricot ?= fruit.
    seeds = VALUE #( ( apricot->kernel ) ).
endcase.

Since 7.40, the constructor operator CAST may be used:

DATA fruit TYPE REF TO zcl_fruit.

...

case fruit->type.
  when 'apple'.
    seeds = CAST zcl_apple( fruit )->seeds.
  when 'apricot'.
    seeds = VALUE #( ( CAST zcl_apricot( fruit )->kernel ) ).
endcase.
like image 70
SteD Avatar answered Sep 23 '22 02:09

SteD