What is # symbol in ABAP?

Viewed 103

I found a piece of code like in the picture, in this case I know more or less what it causes, but generally I don't know the #( ) syntax. What is the #( ) syntax and where can I find more about it?

DATA: lv_str TYPE string VALUE 'ABCD'. 
DATA: dref1 TYPE REF TO data. 
DATA: dref2 TYPE REF TO data. 


* Old Syntax 
GET REFERENCE OF LV_STR INTO dref1. 
* New Syntax 
dref2 = REF #( LV_STR ). 
BREAK-POINT.
3 Answers

The # is a placeholder for the type of the variable. You could write line 11 as this as well:

dref2 = REF data( lv_str ).

That would do the same thing. The # automatically takes the type of the variable on the left, if you don't specify it.

I've not seen it with TYPE REF TO before this, but it's fairly common as VALUE #( )

I haven't found any documentation on the REF #( ) version but here is the official SAP documentation for VALUE #( ), it explains what the # does too.

The documentation of the Reference Operator REF states:

The # character for a data type that is determined by the following hierarchy:

  • If the data type required in an operand position is unique and known completely, the operand type is used.
  • If the operand type cannot be derived from the context, the data type of dobj is used.
  • If the data type of dobj is not known statically, the generic type data is used.

And also, I think that the "mother" documentation chapter is Constructor Operators for Constructor Expressions:

If the data type required in an operand position is unique and can be identified completely, the # character can be used instead of an explicit type specification type and the operand type is used.

If the operand type is not unique and is not known completely, if possible, a type inference is performed to determine a data type.

This is described in each constructor expression.

(NEW, VALUE, CONV, CORRESPONDING, CAST, REF, EXACT, REDUCE, FILTER, COND, SWITCH)

Related