Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Variadic Functions in ABAP?

thomas_bonk
Product and Topic Expert
Product and Topic Expert
0 Likes
2,082

Hello,

is it possible to define variadic method sin ABAP-OO?

Thanks & Best regards,

Thomas

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,038

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.

12 REPLIES 12
Read only

Former Member
0 Likes
2,039

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.

Read only

matt
Active Contributor
0 Likes
2,038

I've used optional parameters when I've needed that kind of functionality.

Read only

0 Likes
2,038

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

Read only

matt
Active Contributor
0 Likes
2,038

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!).

Read only

0 Likes
2,038

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

Read only

matt
Active Contributor
0 Likes
2,038

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 ) ).

Read only

0 Likes
2,038

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

Read only

matt
Active Contributor
0 Likes
2,038

I wonder if as any ideas! 🙂

Read only

0 Likes
2,038

Yeah, Horst is extremely welcome in this thread 😉

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
2,038

nmax, nmin are other built-in functions.

Read only

0 Likes
2,038

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

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
2,038

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.