YOUR NEXT DISCOVERY/How-to & learningFollow your curiosity
Generated painted illustration of an open unlettered book beside amber and indigo modules fitted into separate shaped compartments.
generated illustration · Based on the cited explanation.

Online / Living resource

A value’s permitted range can become part of the program.

Ada Programming

The Wikibooks Ada guide shows how declarations can express a value’s intended kind and permitted range. A small calendar example reveals both the usefulness and the limits of those choices.

A number can fit its permitted range and still be wrong for the task. Following that gap makes type declarations a way to think about the problem a program is meant to represent.

SERENDIPITYA little unexpected
FamiliarWeird

Ada Programming is a community-written guide with separate chapters on the type system and packages. One useful route into the book is to ask what a declaration tells the compiler about a value. The Type System chapter develops that question through distinct types and constrained subtypes.

Consider a day-of-month value constrained to the integers from 1 through 31. That range makes a specific rule visible in the program. AdaCore’s introduction demonstrates how programmers define numeric ranges; the Wikibooks chapter explains that range checks can raise Constraint_Error during execution. Checking a value is therefore part of the program’s behavior, and some checks occur while it runs.

The chapter also distinguishes a new type from a subtype. Creating distinct integer types for quantities such as distance and duration gives them different type identities. A direct assignment between those distinct types is rejected without the required explicit conversion. A subtype places constraints on an existing type, retaining that underlying identity. The choice expresses what kinds of values the programmer intends to keep separate.

The calendar example exposes a boundary of this approach: 31 lies within the chosen range even when the month is February. Calendar validity also depends on the month and year. The range captures one useful condition; a fuller model needs additional rules. This is an original example for reading the tutorial, with no claim that the book supplies a complete calendar implementation.

The generated illustration uses fitted modules as a metaphor for declared boundaries. It is conceptual artwork. The source chapters provide the technical explanation and examples; the image is not a specification or evidence that a program is correct.

Educational discussion adapted from Wikibooks contributors · CC BY-SA 4.0 · Source credits · License and reuse terms

WikibooksAda Programming: reviewed book revision 4635629WikibooksType System: reviewed chapter revision 4665980AdaCoreIntroduction to Ada: Strongly typed language

Source excerpt credited above. Images carry their own reuse terms.