> I'm sidestepping the issue of Ada string handling, which can get complicated: you have fixed-size, bounded, and unbounded strings. Usually you have to read in exactly the amount of characters in the fixed string (in this case three), but with this particular version of Get_Line you can read anything up to the maximum length, while the rest is left alone. That is why I initialize Guess to three blank spaces. Once we start actually reading numbers this will become a non-issue (or a different issue).
One easy way around this (and is often used when parsing a file line-by-line) is to take advantage of Ada's scoping of new variables.
For example:
loop
Put_Line ("Please enter a line.");
declare
New_Data : constant String := Get_Line;
begin
Put_Line ("You said " & New_Data);
exit when New_Data = "quit";
end;
end loop;
This insures that you are only using variables to the specific scope that you need (and I tend to declare that has constant unless you plan on modifying them).
Also, for anyone interested in getting into Ada, something I mentioned in a previous comment: yes easier than ever to get into Ada these days. There's a bunch of tutorials on Adacore's website [1] with a playground/sandbox that you can mess with. With Alire[2] (which is to Ada what Cargo is to Rust), you can get the toolchain and any packages with a simple command. If you're on linux or mac, you can also use GetAda[3] that will automatically install Alire for you.
Exactly how I use this as well :-)
Once you get used to this kind of declaration some things that could be tricky because you need to k ow the exact memory needed suddenly become very easy.
Is this input validation an exceptional error or code flow?
Ada has 'Valid and so you can also do e.g.
If Guess'Valid then
...
In order to check that the data is valid for the types range whilst avoiding the exception that would occur if otherwise read whilst containing invalid data.
Gnat also has the non standard 'Valid_Scalars for records.
Which will check every scalar type in a record. This record may have been filled by unchecked_conversion which ironically does check the size at compile time. It is all very neat along with record overlays. I can't stress enough how far ahead of the competition Ada is for handling hardware registers whether memory mapped or received.
Where is the link for pattern matching as I am unsure of the necessity of it in Ada? I prefer Adas features to Darts new pattern matching atleast for json validation. I do not otherwise have extensive experience of Darts pattern matching though or any of Rusts.
3 comments
[ 3.7 ms ] story [ 11.9 ms ] threadOne easy way around this (and is often used when parsing a file line-by-line) is to take advantage of Ada's scoping of new variables.
For example:
This insures that you are only using variables to the specific scope that you need (and I tend to declare that has constant unless you plan on modifying them).Also, for anyone interested in getting into Ada, something I mentioned in a previous comment: yes easier than ever to get into Ada these days. There's a bunch of tutorials on Adacore's website [1] with a playground/sandbox that you can mess with. With Alire[2] (which is to Ada what Cargo is to Rust), you can get the toolchain and any packages with a simple command. If you're on linux or mac, you can also use GetAda[3] that will automatically install Alire for you.
[1] https://learn.adacore.com/
[2] https://alire.ada.dev/docs/#first-steps
[3] https://github.com/AJ-Ianozi/getada/tree/main
Ada has 'Valid and so you can also do e.g.
If Guess'Valid then ...
In order to check that the data is valid for the types range whilst avoiding the exception that would occur if otherwise read whilst containing invalid data.
Gnat also has the non standard 'Valid_Scalars for records.
Which will check every scalar type in a record. This record may have been filled by unchecked_conversion which ironically does check the size at compile time. It is all very neat along with record overlays. I can't stress enough how far ahead of the competition Ada is for handling hardware registers whether memory mapped or received.
Where is the link for pattern matching as I am unsure of the necessity of it in Ada? I prefer Adas features to Darts new pattern matching atleast for json validation. I do not otherwise have extensive experience of Darts pattern matching though or any of Rusts.