5 ms·
I like how in ada you can specify custom range integers
by Cloudef 5y ago
I like how in ada you can specify custom range integers
- wk_end 5y agoI've always wondered - does Ada track or enforce these ranges? Like, if I have a variable `i` in range 0...10 and write the Ada equivalent of `i++`, does Ada know that from there on out `i` is in range 1...11? Something like TypeScript's type narrowing?
- Cloudef 5y agoYes, it's enforced by any _runtime_ operation. See: https://www.adaic.org/resources/add_content/standards/05rm/html/RM-3-5-4.html https://www.adaic.org/resources/add_content/standards/05rm/h...
- Jtsummers 5y agoThe range wouldn’t change in Ada. The type for a variable is fixed at compile time. It will ensure (at runtime and partially at compile time) that the value doesn’t go outside the range, though.
- throwaway81523 5y agoUsually with runtime checks, but it can also in some situations be done statically, such as with SPARK.
- tasty_freeze 5y agoPascal had subrange types before Ada, not only for integers but characters too. var day : 1 .. 31; letter : 'A' .. 'Z'; And you could specify ranges on array subscripts as well: var weight : array[-5 .. 5] of integer;
- Cloudef 5y agoAh yes, it's been a while since I've touched pascal.. Good times
- elcritch 5y agoNim borrows from both. It's nice using it for embedded work: proc set_mode(mode: range[0..3]) ## SPI mode (0-3)
- mseepgood 5y agoWirth removed this again in Oberon, because experience with Pascal and Modula showed that it is not worth it.
- open-source-ux 5y agoThat's very interesting. Subranges is a feature that I have always liked in Pascal and other languages. You do have any more detail about the reasoning why Wirth thought this feature was "not worth it" for Oberon? Were programmers not using this feature in Pascal and Modula-2? Did Wirth consider it more noise or complexity in those languages?
- deleted 5y ago[deleted]
- tored 5y agoFound this paper "From Modula to Oberon" by Wirth himself http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.578.3109&rep=rep1&type=pdf http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.578... "Subrange types were introduced in Pascal (and adopted in Modula) for two reasons: (1) to indicate that a variable accepts a limited range of values of the base type and to allow a compiler to generate appropriate guards for assignments, and (2) to allow a compiler to allocate the minimal storage space needed to store values of the indicated subrange. This appeared desirable in connection with packed records. Very few implementations have taken advantage of this space saving facility, because the additional compiler complexity is very considerable. Reason 1 alone, however, did not appear to provide sufficient justification to retain the subrange facility in Oberon." "... was the observation that in a growing number of programs the indiscriminate use of enumerations (and subranges) had led to a type explosion that contributed not to program clarity but rather to verbosity." Instead the proposed solution is to use SET. "With the absence of enumeration and subrange types, the general possibility of defining set types based on given element types appeared as redundant. Instead, a single, basic type SET is introduced, whose values are sets of integers from 0 to an implementation-defined maximum."
- 5y ago
- andi999 5y agoNow let's say your name includes one of these weird 'Ä' characters...
- adrian_b 5y agoYes, but Ada extended the number of usable types for ranges, e.g. including fixed-point numbers. All those that existed in Pascal also exist in Ada.
- riffraff 5y agoother than "older" languages in the Pascal family, there is an extended "subset" feature in Raku[0], which is pretty cool. For number ranges you would say subset HandCountable of Int where 1 <= * <= 5 [0] https://ohmycloudy.github.io/24.html https://ohmycloudy.github.io/24.html