Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraints on parameter to implement two interfaces

Tags:

c#

generics

abap

I'm trying to understand some workings of Abap-OO.

In C# it is possible to restrict a type to be any type but conform at least to certain (multiple) interfaces through constraints in generics by doing:

where T : IAmInterfaceA, IAmInterfaceB

Is it possible to archive the same in abap-oo? I want to pass in any object as a parameter to a method that conforms to two interfaces.


For example I want to have those two interfaces:

  • IValidate
  • ISaveable

I do not want to have an extra interface combining the methods those two provide separately.

For example there could be a manager class that wants to save objects but only if they're valid:

Manager.Save(/* <object that conforms to both interfaces IValidate and ISaveable> */ );

So if I got a simple class like SimpleData : IValidate, ISaveable objects of this class could be passed into the method but another object whose class only implements ISaveable cannot be passed in.

In C# I would simply define the save method as a generic method:

static bool Save<T>(T dataObject) where T : IValidate, ISaveable { /* ... */ }

How to do this in abap-oo, if possible?

like image 215
The-First-Tiger Avatar asked Sep 29 '22 09:09

The-First-Tiger


1 Answers

In ABAP, you have to either create a combining interface or check the condition at run-time (which I would NOT advise). You can only use a single type for a parameter. More complex constraints like the ones you quoted are not possible as far as I know.

like image 85
vwegert Avatar answered Oct 06 '22 20:10

vwegert