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

abusemind picture abusemind · Aug 3, 2009 · Viewed 24.4k times · Source

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

Answer

SteD picture SteD · Aug 3, 2009

?= 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.