Lesser-known language features
What you'll learn
IS INSTANCE OF guards a downcast against a class OR interface so it never dumps, SWITCH compares an operand against WHEN values of any comparable type (strings included), and CDS has built-in conversion functions most devs still call FMs for.
- IS INSTANCE OF accepts a class OR an interface — guard CASTs with it to avoid CX_SY_MOVE_CAST_ERROR.
- SWITCH compares an operand against WHEN values of any comparable type (character fields and strings included), collapsing IF/ELSEIF chains.
- CDS `cast( expr as abap.fltp )` forces DB-side float casts — the fix for integer-division precision.
IS INSTANCE OF is the type-safe gate for downcasts, and the detail most developers miss is that it accepts a *class or an interface*. Guarding a CAST with `if obj is instance of zif_serializable` lets you narrow to an interface reference without risking the CX_SY_MOVE_CAST_ERROR that a naked CAST throws when the object doesn't implement it. It turns a runtime exception into a branch you control.
Two more expressions remove boilerplate. SWITCH evaluates an operand against a list of WHEN values and works with any comparable type — including character fields and strings, not just numeric or enumerated values — so a chain of IF/ELSEIF on a character field collapses into one readable expression. And in CDS, `cast( expr as abap.fltp )` forces the cast on the database side — the canonical use is integer division, where casting an operand to floating point before dividing avoids the truncation you would otherwise get from integer arithmetic pushed to HANA.
CDS also ships SQL functions that replace whole function-module calls. currency_conversion( ) and unit_conversion( ) do on the database what most ABAP still does by calling BAPI_CURRENCY_CONV_TO_INTERNAL; concat_with_space( ) joins strings with a separator; and coalesce( arg1, arg2 ) returns arg1 if it is not null, otherwise arg2 — in CDS it takes exactly two arguments (ABAP SQL's coalesce is the variadic form that accepts 2 to 255). `let ... in` lets you name a reusable expression once inside a single view. Reaching for these keeps logic declarative and out of ABAP loops.
Key points
- IS INSTANCE OF accepts a class OR an interface — guard CASTs with it to avoid CX_SY_MOVE_CAST_ERROR.
- SWITCH compares an operand against WHEN values of any comparable type (character fields and strings included), collapsing IF/ELSEIF chains.
- CDS `cast( expr as abap.fltp )` forces DB-side float casts — the fix for integer-division precision.
- CDS built-ins currency_conversion / unit_conversion / concat_with_space / coalesce (two-argument in CDS) push logic down instead of calling FMs.
Examples
The guard means the CAST only runs when the object really implements the interface — no move-cast dump.
ABAPif lo_object is instance of zif_serializable.
data(serial) = cast zif_serializable( lo_object ).
endif.One expression replaces an IF/ELSEIF ladder over a character field.
ABAPdata(lv) = switch string( country
when 'CA' then 'Canada'
else 'Other' ).Source notes: clean-core-curriculum §11.1
Ask Claude
Build a prompt from this lesson + your question and open a fresh Claude chat with it pre-filled — handy for adapting a before/after pattern to your own object.