‎2014 Oct 20 2:02 PM
Hello,
is it possible to define variadic method sin ABAP-OO?
Thanks & Best regards,
Thomas
‎2014 Oct 20 3:07 PM
Hi, Thomas!
I don't think it is possible in a way it is done in C, for example.
Depending on your needs you can mark some arguments as optional (but you'll have to define them).
If it's not enough, maybe sending some table of input parameters (e.g., name, type, value) will help you. But I don't think it's a nice solution Maybe community gurus can advise a more elegant way of accomplishing your task.
‎2014 Oct 20 3:07 PM
Hi, Thomas!
I don't think it is possible in a way it is done in C, for example.
Depending on your needs you can mark some arguments as optional (but you'll have to define them).
If it's not enough, maybe sending some table of input parameters (e.g., name, type, value) will help you. But I don't think it's a nice solution Maybe community gurus can advise a more elegant way of accomplishing your task.
‎2014 Oct 20 3:51 PM
I've used optional parameters when I've needed that kind of functionality.
‎2014 Oct 20 4:45 PM
Hi Matthew,
the difference between variadic functions or methods and optional parameters is, that you have do define the optional parameters beforehand. I contrast to that, variadic functions enable you to provide additional parameters without specifying how many optional parameters could occur during the definition of the function or method.
I think Nikolay is right, there is no elegant solution for variadic functions in ABAP.
Christian
‎2014 Oct 21 7:43 AM
The nearest then will be internal tables. But I'd probably implement them as just another class. Do you have a particular scenario in mind? (I extensively program in other languages that do have variadic functions and have so far never actually needed them!).
‎2014 Oct 21 9:33 AM
Hi Matthew,
yes, I think an optional internal table could be a good substitute in many situations.
IMHO you would mostly need variadic functions if you try to build some kind of framework. The most natural candidates for variadic functions I can thinks of are e.g. concat, min or max. These are implemented in Clojure as variadic functions.
For some of these (concatenate) there are build in language constructs in ABAP. To implement min or max you would most likely use an internal table to provide the arguments.
Christian
‎2014 Oct 21 9:44 AM
I'm not certain of the syntax, but for a max function, something like this would probably work (in 7.40)
If I have i,j and k, all integers, and I want the max, I can create a method max, that takes an internal table of integers as importing parameter (and returns the max). I can then call max like this:
data(maximum) = my_class->max( VALUE #( ( i ) ( j ) ( k ) ).
‎2014 Oct 21 10:16 AM
Hi Matthew,
interesting idea. This way one at least doesn't need to create additional auxiliary variables (at least not explicitly). I'll give it a try.
However, the problem I see with this approach is that I always have to provide the additional constructor expression when invoking the method.
Christian
‎2014 Oct 21 10:33 AM
‎2014 Oct 21 10:36 AM
‎2014 Oct 21 11:17 AM
‎2014 Oct 21 12:15 PM
Hi Horst,
But even nmax and nmin are not variadic, right? They only allow for up to 9 arguments. If I'd like to have a function that also works with 10 or 11 parameters I'd have to use a different approach.
I guess the real question is if there is an idiomatic approch in ABAP (e.g. with some of the features in 7.40) that allows the creation of variadic functions or methods.
Christian
‎2014 Oct 21 12:22 PM
if there is an idiomatic approch in ABAP (e.g. with some of the features in 7.40) that allows the creation of variadic functions or methods
Sorry, none that I know of. Only the means of generic/dynamic programming discussed already here.