Local Classes in Reports and Definition Deferred
I saw an SCN forum post today that required clarification, but that post was so old that there is no way that I could post there even if it was possible…
The question was raised there about using a locally defined class in a report where the original poster did not like that they cannot return a locally defined type to contain their specific object, for example a table type containing just their object / ie a collection of their object.
The post is contained at the following link
When I found this page I was aimlessly searching to understand why I couldn’t create a builder pattern in ABAP. To give some background, I wanted to have a local class which will construct the parent class using a builder to see how that might look. The answer is that it looks horrible, in case you are wondering, but also that its of little value anyway because simultaneously learnt that the forward definitions of a global class are not accessible in the ‘[public|protected] section’ of a class. I will attempt to describe this later but first, I want to clarify this post. If you use the ‘definition deferred’ keywords you can definitely use the object you are about to construct in a return type of some kind. Now i will say that it is probably always better to use a global class since your functionality could be reusable if you build it in a sensible fashion… And I will go even further to add that I feel the best use of a local class is as an internal implementation of an interface or for some functionality specific to your particular class, similar to how you may use nested classes in other languages.
So anyhow, the following is an example that does return from a method a table type of that particular local class object. And the use of the generic ‘object’ type is not necessary.
Anyway I just wanted to post this since I cant really reply to that thread. Regarding my previous comments, there is a restriction with any forward defined class that it can not be found as a valid type for use in either the PUBLIC SECTION, or PROTECTED SECTION blocks… the variable can only be defined in the PRIVATE SECTION… and this is not good. I assume this is done intentionally to ‘protect’ developers from exposing local classes outside of their object… unfortunately that restriction does block some patterns(even if they do look horrific in practice).
P.s. sorry I don’t have an example because it would simply be an example of something impossible failing to work (at least when trying to use a local class that is).