www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: ranges-static property 'valid'

reply Achilleas Margaritis <Achilleas_member pathlink.com> writes:
Ranges:
-------

The 'typedef' declaration could also be used to introduce new numeric types with
user-defined ranges:

typedef:
typedef decl RangeDecl

RangeDecl:
( Min .. Max )

Example:
typedef int RadarRange(100 .. 20000) = 500;


The .min and '.max' properties of the new type will be adjusted accordingly by
the compiler.


Static property 'valid':
------------------------

A type (primitive/enum/struct/class/array) could have a static property 'valid'
which returns 'true' when the content of the variable that it is applied to is
valid.

Example:

RadarRange currentRadarRange;
bool isValidRange = currentRadarRange.valid;

The .valid static property would be useful for checking at run-time if a value
is valid (within range/accepted values). The .valid check would propagate in
composite type's members, thus allowing for checking whole
structs/classes/arrays if they have valid content.

It would save the programmer lots of work from checking if an enumerated value
has a valid content.


Rationale:
----------

D could be used in place of ADA for defense applications (I could certainly
propose it to my boss if it had the above-mentioned capabilities!).
May 12 2004
parent "C. Sauls" <ibisbasenji yahoo.com> writes:
I actually proposed an idea similar to this many months ago.  My 
proposed syntax was rather different, but the concept was essentially 
the same.  I had it set to look something like:

------------------------------
typedef int myint {
   init : -1,
   min  : -3,
   max  : int.max
}
------------------------------

I'd also proposed the idea of adding new properties to the typedef'd 
type.  Maybe your proposal will get more attention than mine.

-C. Sauls
-Invironz
May 12 2004