将类型声明为集合的子集 [英] Declaring a type as a subset of a set

查看:84
本文介绍了将类型声明为集合的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以轻松地声明一个枚举和一个集合。

但是有时候我只想使用一部分枚举,我希望编译器为我检查子枚举中的值

I can easily declare a enumeration and a set.
But sometimes I want to work with only part of the enumeration and I'd like the compiler to check for me if values in the sub-enum and its subset stay within the bounds.

type
  TDay = (mon, tue, wen, thu, fri, sat, sun);
  TWeekday = (mon..fri); //not allowed;

  TDays = set of TDay;
  TWeekdays = set of TDay[mon..fri]; //not allowed

我可以声明 TWeekday TWeekdays 作为TDay的衍生产品,如果是的话,如何?

Can I declare TWeekday and TWeekdays as a derivative of TDay, if so, how?

足够有趣的google在这个问题上不会产生任何收益(对我而言),只是简单的旧集。

推荐答案

您对该子范围的语法有误。放下括号()即可。

You've got the wrong syntax for the subrange. Drop the brackets () and it will work.

type
  TDay = (mon, tue, wen, thu, fri, sat, sun);
  TWeekday = mon..fri; // A subrange of TDay

  TDays = set of TDay;
  TWeekdays = set of TWeekDay; 

有关子范围类型集合

这篇关于将类型声明为集合的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆