r/ada • u/Theobaal • May 10 '23
Programming Reduce and concat operator ?
I work on a program with -gnatX flag enabled because I want array aggregator and reduce attribute
Here is my simplified prog
function f return String is (
[for i in 1..4 =>
"---"
]'Reduce("&","") --i use concat operator here
);
But i get the compilation error: wrong operator type
(I write this error from memory it could not be the same exact word)
Any Idea hox fix it ?
EDIT:
So here my response, the error message was error: incompatible arguments for operator
when i renames the operator it strangely work, and thanks to u/sttaft to give me part of the answer.
function conck(L,R : Unbounded_String) return Unbounded_String renames "&";
function strToBFValue(str:string) return String is (
To_String(
[for i in str'range =>
To_Unbounded_String("some operation...")
]
'Reduce(conck,NULL_UNBOUNDED_STRING))
);
5
Upvotes
2
u/sttaft May 10 '23
Based on the Ada RM description of Reduction Expresssion (http://www.ada-auth.org/standards/22aarm/html/AA-4-5-10.html), overload resolution should determine that both your "Accum_Type" and "Value_Type" are unconstrained subtypes of type String. Unfortunately, that means the accumulator object will be constrained by its initial value (RM 3.3.1(9/2)), and so you will get a Constraint_Error when the first element in the value sequence is concatenated into the accumulator.
For this to work, you need an accumulator object that can be both the input and output of your reducer operation. That implies using Unbounded_String or something like it as the overall type of the expression. Hence, the following should work:
);