Search code examples
oopgenericsmethodsparametersabap

Partly static and partly dynamic method parameter?


I need a method parameter with a partly generic type: some of its components should be known statically, and the rest can be generic.

Currently, my class method has an importing parameter is_param type simple which is fully generic. I would like to force the parameters passed to that method to have at least the components a, b, c in their type. Components a, b, c should be accessible statically inside the method like this: is_param-a, is_param-c, is_param-c.

The other components of the parameter will be dynamic.

How to type such a parameter ?


Solution

  • Unfortunately there is no way to have a parameter that is partially dynamic and partially static. So you're stuck with the dynamic parameter is_param. You can check the parameter for the components a b and c via assign component 'A' of structure is_param to <value>. and if you have a sy-subrc <> 0 raise an exception.

    To access a, b and c statically (without assigning them every time) you could just define a structure with these components and move the fields over from is_param. Then you can use that structure whenever you need access to those fields.

      method do_something.
    
        data: begin of ls_static,
                 a type char1,
                 b type char1,
                 c type char1,
               end of ls_static.
        move-corresponding is_param to ls_static.
      *  use ls_static for a,b and c...
        
      *  if it's a changing parameter move a,b and c back:
        move-corresponding ls_static to is_param.
      endmethod.
    

    If is_param is a changing parameter you have to move them back to from ls_static at the end. In this case you have to be careful not to change a,b,c in is_param or use them from there then since you could have different states in is_param and ls_static then.

    Note: There is maybe a way to use a class to achive this since you can access global attributes with object->attribute. So you could use this to access your static components and store the rest of the data in that class too. I gave it a try but couldn't find a good way to actually achive anything that's easier to use or more convenient then the solution above.