Question

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

Was it helpful?

Solution

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

OTHER TIPS

It is Casting operator (?=) for assignments between reference variables,but specifically speaking it is down casting operator .

?= is used to refer to a super class object by its inherited class object in the form

[object reference of parent class] ?= [object reference of inherited class]

This is useful when the type resolution occurs at runtime. While ?= can be specified for upcasts also, it is not usually necessary.

?= is used to type cast an object reference of an inherited class to an object of the super class from which it is derived.

?=

Type casting helps you to refer several object references of sub classes whose type is resolved only at run time. The parent class object reference can hold the objects and often there would be a method of parent class which can be used to determine what sub class object the type cast reference is holding at run time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top