Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Widening casting-code

Former Member
0 Kudos

class parent definition.

public section.

data pub_parent type i.

endclass.

class child definition inheriting from parent.

public section.

data pc type i.

endclass.

data parent type ref to parent.

data child type ref to child.

data child1 type ref to child.

create object child.

create object parent.

try.

parent ?= child.

catch cx_sy_move_cast_error.

write 'error'.

endtry.

-


Hi all

code snippet above doesnt gives me any error..I am doing narrow casting here with a widening cast operator ?=.does that mean ,when it comes to narrow casting u can use either = or ?=.it doesnt make any difference?

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
0 Kudos

widening cast : more_general = less_general

narrowing cast : less_general ?= more_general

3 REPLIES 3

Sandra_Rossi
Active Contributor
0 Kudos

widening cast : more_general = less_general

narrowing cast : less_general ?= more_general

0 Kudos

I am referring to taw10_3 material,in which they say

more_gen = less_gener--->narrow cast, since then,acces of more_gen is limited to shared comp.only

less_gen ?= more_general---widening cast

Amnt sure whether they renamed the terminology(widening and narrow).

Here my doubt wasnt what is narrow and widening.i

But i could use the ?=,operator for assigning a less_gen to a more_gen ref.variable.why?

0 Kudos

I'll try to explain this using an example.

DATA: go1 TYPE REF TO object,      "More generic
      go2 TYPE REF TO zcl_myclass. "Less generic

When you try to assign a 'more-generic' ref. to a 'less generic' ref., the static check performed by the compiler throws a compilation error.

* Assign a more generic ref. to less generic ref.
go2 = go1.

The compiler checks the static types of go1 & go2 and since the assignment is not compatible(more generic is being assigned to less generic) syntax error is raised.

The casting operator '?=' is a kind of instruction to the compiler to skip the static check & perform the checks during runtime!

CREATE OBJECT go1 TYPE zcl_myclass.

TRY .
* Static compiler check is skipped. The compatibility is checked only
* at runtime
    go2 ?= go1.
    MESSAGE 'Casting is successful' TYPE 'S'.
  CATCH cx_sy_move_cast_error.
    MESSAGE 'Error in casting' TYPE 'E'.
ENDTRY.

The compiler checks the type of go1 at runtime & since the assignment is compatible no exception is raised.

Consider this example, the exception CX_SY_MOVE_CAST_ERROR is raised at runtime since the dynamic types are not compatible!

DATA: gx_err  TYPE REF TO cx_sy_move_cast_error,
      gv_msg  TYPE string.

CREATE OBJECT go1 TYPE zcl_airlines.

TRY .
* Static compiler check is skipped. The compatibility is checked only
* at runtime
    go2 ?= go1.
    MESSAGE 'Casting is successful' TYPE 'S'.
  CATCH cx_sy_move_cast_error INTO gx_err.
    gv_msg = gx_err->get_text( ).
    MESSAGE gv_msg TYPE 'S'.
ENDTRY.

But i could use the ?=,operator for assigning a less_gen to a more_gen ref.variable.why?

The casting operator '?=' can be used for both narrowing as well as widening cast. But in case of widening cast, its use is redundant.

Hope you get my point.

BR,

Suhas

Edited by: Suhas Saha on Jun 13, 2011 5:16 PM