Subrange types

A subrange type represents a subset of the values in another ordinal type (called the base type). Any construction of the form Low..High, where Low and High are constant expressions of the same ordinal type and Low is less than High, identifies a subrange type that includes all values between Low and High. For example, if you declare the enumerated type

type TColors = (Red, Blue, Green, Yellow, Orange, Purple, White, Black);

you can then define a subrange type like

type TMyColors = Green..White;

Here TMyColors includes the values Green, Yellow, Orange, Purple, and White.

You can use numeric constants and characters (string constants of length 1) to define subrange types:

type
  SomeNumbers = -128..127;
  Caps = 'A'..'Z';

When you use numeric or character constants to define a subrange, the base type is the smallest integer or character type that contains the specified range.

The LowerBound..UpperBound construction itself functions as a type name, so you can use it directly in variable declarations. For example,

var SomeNum: 1..500;

declares an integer variable whose value can be anywhere in the range from 1 to 500.

The ordinality of each value in a subrange is preserved from the base type. (In the first example, if Color is a variable that holds the value Green, Ord(Color) returns 2 regardless of whether
Color is of type TColors or TMyColors.) Values do not wrap around the beginning or end of a subrange, even if the base is an integer or character type; incrementing or decrementing past the boundary of a subrange simply converts the value to the base type. Hence, while

type Percentile = 0..99;
var I: Percentile;
...
I := 100;

produces an error,

...
I := 99;
Inc(I);

assigns the value 100 to I (unless compiler range-checking is enabled).

The use of constant expressions in subrange definitions introduces a syntactic difficulty. In any type declaration, when the first meaningful character after = is a left parenthesis, the compiler assumes that an enumerated type is being defined. Hence the code

const
  X = 50;
  Y = 10;
type
  Scale = (X - Y) * 2..(X + Y) * 2;

produces an error. Work around this problem by rewriting the type declaration to avoid the leading parenthesis:

type
  Scale = 2 * (X - Y)..(X + Y) * 2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值