null and type safety

Michel Fortin michel.fortin at michelf.com
Thu Nov 6 02:48:10 PST 2008


On 2008-11-05 08:16:59 -0500, bearophile <bearophileHUGS at lycos.com> said:

> The same is true making integral values become range values. If I want 
> to write a function that takes an iterable of results of throwing a 
> dice, I can use an enum, or control every item of the iterable for 
> being in range 1 - 6. If range values are available I can just:
> 
> StatsResults stats(Dice[] throwing_results) { ...
> 
> Where Dice is:
> typedef int:1..7 Dice;
> 
> I then don't need to remember to control items for being in 1-6 inside 
> stats(), and the control is pushed up, toward the place where that 
> throwing_results was created (or where it comes from disk, user input, 
> etc). This avoids some bugs and reduces some code.

It's exactly the same thing, except that for numbers you may want much 
more than simple ranges. You could want non-zero numbers, odd or even 
numbers, square numbers, etc. I have the feeling that whatever the 
language try to restrict about numbers, it will never be enough. So my 
feeling is that this is better left to a template.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list