Skip to content

Instantly share code, notes, and snippets.

@js1972
Created October 1, 2014 06:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save js1972/7b3eef273ef686583454 to your computer and use it in GitHub Desktop.
Save js1972/7b3eef273ef686583454 to your computer and use it in GitHub Desktop.
Some new ABAP 7.40 example code
report y_test_abap_740.
interface lif_test.
methods:
print
importing msg type string.
endinterface.
class lcl_test definition.
public section.
interfaces: lif_test.
endclass.
class lcl_test implementation.
method lif_test~print.
write: / msg.
endmethod.
endclass.
data lo_object type ref to lif_test.
start-of-selection.
lo_object = new lcl_test( ).
lo_object->print( msg = 'Hello World. This way is good as we get a refrernce to the interface!' ).
new lcl_test( )->lif_test~print( msg = 'Anonymous object').
data(inline) = new lcl_test( ).
inline->lif_test~print( msg = 'Inline declaration' ).
data inline_if type ref to lif_test.
inline_if ?= inline.
inline_if->print( msg = 'We need to typecast to get a reference to the interface. Don''t like that!' ).
data inferred type ref to lcl_test.
inferred = new #( ).
inferred->lif_test~print( msg = 'Class is inferred with new #( )' ).
data inferred_if type ref to lif_test.
inferred_if ?= inferred.
inferred_if->print( msg = 'Again we need to typcast to get a reference to the interface' ).
" BOOLEANS
data(flag) = boolc( sy-batch is not initial ).
write: / 'In batch mode: ', flag.
" ITABS
write: 'ITABS:'.
data itab type table of i.
itab = value #( ( 1 ) ( 2 ) ( 3 ) ).
loop at itab assigning field-symbol(<val>).
write: / <val>.
endloop.
" The following is available from SP08 - "BASE" allows you to base a new structure on another.
"itab = VALUE #( BASE itab ( 4 ) ( 5 ) ( 6 ) ).
" Structures
skip.
data:
begin of struct1,
col1 type i value 11,
col2 type i value 12,
end of struct1.
data:
begin of struct2,
col2 type i value 22,
col3 type i value 23,
end of struct2.
struct2 = corresponding #( struct1 ).
write: / struct1-col1, struct1-col2.
write: / struct2-col2, struct2-col3.
" Note: col3 above is initialised!
"struct2 = VALUE #( BASE struct1 col3 = 33 ).
" FOR COMPREHENSIONS IN ABAP: REDUCE IS ONLY AVAILABLE from SP08!!!
"skip 2.
"TYPES outref TYPE REF TO if_demo_output.
"
"DATA(output) = REDUCE outref( INIT out = cl_demo_output=>new( ) text = `Count up:` FOR n = 1 UNTIL n > 11 NEXT out = out->write( text ) text = |{ n }| ).
"output->display( ).
" BASIC FOR COMPREHENSIONS DO WORK NOW THOUGH:
skip 2.
types: begin of t_line,
col1 type i,
col2 type i,
col3 type i,
end of t_line.
types: t_itab type table of t_line with empty key.
data(itab1) = value t_itab( ( col1 = 10 col2 = 20 col3 = 30 )
( col1 = 20 col2 = 30 col3 = 40 )
( col1 = 30 col2 = 40 col3 = 50 )
( col1 = 40 col2 = 50 col3 = 60 ) ).
data(itab2) = value t_itab( for wa in itab1 where ( col1 < 30 ) ( col1 = wa-col2 col2 = wa-col3 ) ).
loop at itab2 assigning field-symbol(<t2>).
write: / <t2>-col1, <t2>-col2, <t2>-col3.
endloop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment