Application Development and Automation 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: 
Read only

Object casting: confusion in ABAP Objects: Complete Reference book

Former Member
0 Likes
855

Hi,

During Object Assignments using casting, is a Type Test carried out during the syntax check or at runtime?

A.5.2.2 (page 1008) of 'ABAP Objects: The Complete Reference' says about Widening Cast: "...you must check at runtime...". However on the next page under A.5.3.2 it says of Widening Cast in Data References: "The syntax check precludes...".

A.5.4 (page 1010) concerns Assignments between Object Reference Variables, but makes no mention of whether checks are carried out by a syntax check or at runtime.

Also nowhere does it mention when Type Tests for Narrow casting takes place. Can anyone clear my confusion please? Unfortunatly I don't know enough about this stuff to test by writing some code.

Thanks.

1 ACCEPTED SOLUTION
Read only

former_member186741
Active Contributor
0 Likes
715

I believe it does checks at both syntax check and runtime. Sometimes it cannot do the precise check until runtime, for example if you are using field symbols, but it tries to do as much as it can at the syntax check stage.

6 REPLIES 6
Read only

former_member186741
Active Contributor
0 Likes
716

I believe it does checks at both syntax check and runtime. Sometimes it cannot do the precise check until runtime, for example if you are using field symbols, but it tries to do as much as it can at the syntax check stage.

Read only

0 Likes
715

Hi,

Thanks for the reply.

I suspose the core of the question is whether a Widening Cast for an object assignment carries out a Type Test during runtime, or during the syntax check?

Thanks.

Read only

0 Likes
715

Hi William,

I haven't read the book but as far as I know the check is performed at runtime. That's why casting statements a useually followed by a try - catch statement.

Read only

0 Likes
715

Hi,

Thanks for the reply.

Rgds,

William.

Read only

Former Member
0 Likes
715

William,

Your questions can be answered by the following rule for object references, which I found in the book "ABAP Objects" by Horst Keller and Sascha Krüger:

"... that the static type of the target variable must be equal to or more general than the dynamic type of the source variable."

Here "static type" means the type with which an object reference variable is declared. "Dynamic type" is the type that the object reference variable has at runtime. The dynamic type of an object reference is always more special than its static type, otherwise a runtime error occurs.

With this rule all your questions can be answered:

1. The Narrowing Cast is always checked during the syntax check. Example:

DATA o_ref1 TYPE REF TO object.

DATA o_ref2 TYPE REF TO class_1.

...

o_ref1 = o_ref2.

Here the reference o_ref2 has a dynamic type "class_1" or a subclass of it, which is narrower than its static type "class_1", which is narrower than the static type "object" of the reference o_ref1. Therefore, the syntax check says that the assignment is OK.

2. The Widening Cast is always checked at runtime and requires an assignment using the operator ?=. If you use the operator = in the assignment, a syntax error occurs. Therefore the following example produces a syntax error (try it yourself):

DATA o_ref1 TYPE REF TO object.

DATA o_ref2 TYPE REF TO class_1.

...

o_ref2 = o_ref1.

The correction for this syntax error is:

DATA o_ref1 TYPE REF TO object.

DATA o_ref2 TYPE REF TO class_1.

...

o_ref2 ?= o_ref1.

Now the syntax check is satified, and the correctness of the widening cast is checked at runtime.

Kind regards,

Michael Kraemer

Message was edited by: Michael Kraemer

Read only

0 Likes
715

Hi,

Thanks for the very detailed reply.

Rgds,

William.