type OperatingTemp is range 33 .. 90;
And then declare variables of that type and they will be range checked - an exception will be thrown if the variable goes out of that range. Wish more languages had this feature. type OperatingTemp is range 33 .. 90;
And then declare variables of that type and they will be range checked - an exception will be thrown if the variable goes out of that range. Wish more languages had this feature. type Character_Histogram is Array (range 'A'..'Z') of Integer;
-- syntax may be off, not enough practice to do this cold right now type Colors is (Red, Green, Blue, Black, Gray, White);
It has to be a consecutive group of them, taken in order, as the specification of even this enumeration has an ordering to it as far as Ada is concerned. So I can make an array using all of Colors as the index, or some subset but it has to be, say, Red..Blue and not Red|Blue skipping over Green. type
TPerson = record
name: string;
age: integer;
end;
TThrteenPersons = array[10..22] of TPerson;
procedure Check(const aPerson: TPerson);
var
vPerson: TThrteenPersons;
begin
vPerson[11] := aPerson; // this is ok
vPerson[1] := aPerson; // this one gives compile error
end;
alternatively if runtime range checking is on then vPerson[i] = aPerson will raise the exception if i is out of range Histogram : Character_Histogram;
...
Histogram['A'] := Histogram['A'] + 1;
(Ok, a bit more work because I didn't initialize the histogram to 0.)This modern pascal is very feature rich and has plenty of fancy bells and whistles.
So you can have arrays where the index runs from -10 to +20 or from +7 to +26 for instance. Booleans are also ordinal types so you can have an array where the index is boolean.
And finally VHDL is strongly typed. Verilog is pretty much C-typed meaning it's pretty weak. About a dozen years ago I worked at an EDA company and one of my tasks was to run our generated HDL code through a popular industry linting tool. There were hundreds linting problems with the Verilog code that needed to be fixed. In the VHDL code there were only a couple of things that needed to be addressed - this was mostly due to VHDL's strong type system preventing many of the problems that showed up on the Verilog side.
That idea was neatly generalized 30 years ago. So expect it to start showing up as the hot new thing in another 20.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
In Java too, don’t know about other languages though...
The benefit of this in Ada is that it is universal, and it requires the programmer to do nothing special later on except to not turn off the runtime checks. Using that above example, this would be a runtime error:
Reading : Integer := Get_A_Reading(...); -- In some run Reading gets 0 for some reason
Temperature : OperatingTemp;
...
Temperature := OperatingTemp'Value(Reading); -- runtime error in Ada if Reading is invalid
And because OperatingTemp is a distinct type, you never even have the option to do: Temperature := Reading; -- doesn't compile
Also, in most OO languages where you'd make temperature an internal, private variable you can prevent external users of the class from interacting with it incorrectly, but you can't control internal users. Consider this Java-ish example: class Thermostat {
private int temperature; // should only be in [33,90]
public void SetTemperature(int value) { // check range and set }
public void AnotherMethod(int value) {
// good discipline would be:
SetTemperature(f(value));
// but it's not strictly required so this is also allowed
temperature = f(value);
}
}If a civil engineer had access to concrete but he chose to build with mashed potato instead he would be considered insane. Yet programmers with access to Ada choose JavaScript and it's considered perfectly normal.