weird_numbers.adb:47:47: warning: value not in range of type “Ada.Containers.Vectors.Element_Type” from instance at line 11 [enabled by default]
weird_numbers.adb:47:47: warning: Constraint_Error will be raised at run time [enabled by default]
weird_numbers.adb:84:37: warning: value not in range of type “Ada.Containers.Vectors.Element_Type” from instance at line 11 [enabled by default]
weird_numbers.adb:84:37: warning: Constraint_Error will be raised at run time [enabled by default]
I suspected the earlier failure was because the compiler was checking the sum of Positive (the element type) and Natural (the 0 in line 47) and deciding it was invalid. Changing the element type to Natural removes that, but you’re still getting this warning and, presumably, the error?
I’m at a complete loss. I don’t see the warning, nor do I see why I should, and I certainly l don’t encounter the error.
What version of gnat are you running? My toolchain reports
i downloaded gnat 14.1.1 and it does indeed crash with the code on Rosetta Code, but not with the changes i made. can you update to 14.1.1 and see if that fixes your problem? i’ll also fix the entry in Rosetta Code.
Note, added later: Is this a bug? Please answer here.
@Ret_Build_Engineer is using my 14.1.0, which is built from the same source as Alire’s 14.1.1; I find this use of the patchlevel confusing. If it’s built from a tag 14.1.0-1 why not use that as the release ID?
if only A := B is visible, I initially think that there’s a semicolon missing. I have to make an effort to make sure that’s not true. On the other hand, given
A := B +
C;
it’s clear that the statement is incomplete, and I need make no effort, so the second form is easier to read and understand. For that reason, my Coding Standard requires the second form.
Regarding this specific case, punctuation, Ada is designed to read much like English text (presuming identifiers are Composed_Of_English_Words). Commas and semicolons are always attached to the thing that precedes them in English, and so should be attached to the thing that precedes them in Ada.
Regarding Strip_Character, as pointed out, C should be a character [so you’d write Index (S, C & "")], and the name Comma_At is poor, and should probably be C_At.
Once those changes are made, the function is simple, clear, and easy to understand. Changing that to avoid recursion would make it more complex and less clear, and so should only be done for a good reason. I have not seen a good reason for changing Strip_Character. (You should always be wary of those who worry about low-level “performance” for no reason.) It is by no means given that recursion will be slower than the overhead of avoiding it.
Thanks a lot JC, your quidelines are great! I see it’s almost like writing natural English which makes things easier to remember
I guess this is a matter for potential big debates I’ve made the exercise of changing the function to be non recursive and with Ada’s arrays it was easy and quite simple. I still think the recursive solution is more elegant though…
Have you measured them to see if there is a difference worth the effort of avoiding the recursion? If it doesn’t, then simplicity and clarity (“elegance”) should determine the choice.
No, I haven’t, but I’d like to try, for the sake of learnng. By the way, is there a way to measure memory usage in Ada? I think it could be interesting to compare that when processing very long strings, could be enlightening.