简体   繁体   English

在Ada中描述字符串类型

[英]Describing a String type in Ada

I have a type similar to: 我的类型类似于:

type ID is new String (1 .. 7);
-- Example: 123-456

How can I specify that format in code, either with Ada or SPARK? 如何使用Ada或SPARK在代码中指定该格式?

I was thinking about Static_Predicate , but the condition that the string must start with 3 positive integers followed by a dash followed by another set of 3 positive integers can't be described with a Static_Predicate expression. 我当时在考虑Static_Predicate ,但是不能用Static_Predicate表达式来描述字符串必须以3个正整数开头,后接一个破折号和另一组3个正整数的Static_Predicate

You have to use a Dynamic_Predicate for this: 您必须为此使用Dynamic_Predicate

type ID is new String (1 .. 7)
  with Dynamic_Predicate => (for all I in ID'Range =>
                               (case I is
                                   when 1 .. 3 | 5 .. 7 => ID (I) in '0' .. '9',
                                   when 4               => ID (I) in '-'));

I'm using this quite a bit myself, but I mostly make the types subtypes of String instead of actual new types. 我自己经常使用它,但是我主要使String的类型子类型代替实际的新类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM