Why I chose D over Ada and Eiffel
John Colvin
john.loughran.colvin at gmail.com
Thu Aug 29 07:13:06 PDT 2013
On Thursday, 29 August 2013 at 13:33:52 UTC, Gour wrote:
> On Sun, 25 Aug 2013 17:06:27 +0200
> "bearophile" <bearophileHUGS at lycos.com> wrote:
>
>> Probably working even more you can make the D entry a bit more
>> statically safe (eventually you could reach the level of Ada
>> code) but
>> the amount of work and code becomes excessive, and the
>> resulting D
>> code becomes unnatural, and rather not idiomatic.
>
> Still considering whether to focus on Ada or D for my project,
> I wonder
> if D can do stuff like (from wikipedia page):
>
> type Day_type is range 1 .. 31;
> type Month_type is range 1 .. 12;
> type Year_type is range 1800 .. 2100;
> type Hours is mod 24;
> type Weekday is (Monday, Tuesday, Wednesday, Thursday, Friday,
> Saturday, Sunday);
>
> type Date is
> record
> Day : Day_type;
> Month : Month_type;
> Year : Year_type;
> end record;
>
> subtype Working_Hours is Hours range 0 .. 12;
> subtype Working_Day is Weekday range Monday .. Friday;
>
> Work_Load: constant array(Working_Day) of Working_Hours
> := (Friday => 6, Monday => 4, others => 10);
>
>
> and ensure type-safety for such custom types?
>
>
> Sincerely,
> Gour
just something I whipped up in a few mins:
import std.typecons;
import std.exception;
struct Limited(T, T lower, T upper)
{
T _t;
mixin Proxy!_t; //Limited acts as T (almost)
invariant()
{
enforce(_t >= lower && _t <= upper);
}
this(T t)
{
_t = t;
}
}
auto limited(T, T lower, T upper)(T init = T.init)
{
return Limited!(T, lower, upper)(init);
}
unittest
{
enum l = [-4,9];
auto a = limited!(int, l[0], l[1])();
foreach(i; l[0] .. l[1]+1)
{
a = i;
}
assertThrown({a = -5;}());
assertThrown({a = 10;}());
}
This could be a lot more generic than it is. Redesigning
Restricted to hold a pointer to a function that does the check
would be one way.
More information about the Digitalmars-d
mailing list