Ada-like types in Nimrod
Asked Answered
A

2

10

today I did ask in D mailing list whether it's possible to define and use custom data types in a way similar to e.g. example from Ada's wiki 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 the reply demonstrated something like:

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;}());
}

which shows it's possible, but probably misses Ada's elegance.

Now, after reading about Nimrod recently, I wonder how it can handle similar task with the provision to ensure same Ada's type-safety?

Anyone answered 29/8, 2013 at 15:15 Comment(0)
S
7

Nimrod support these rather directly:

type
  Day = range[1..31]
  Month = range[1..12]

  WeekDay = enum
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

  WorkingDays = range[Monday..Friday]
  WorkingHours = range[0..12]

  WorkSchedule = array[WorkingDays, WorkingHours]

Errors are enforced either at compile-time:

var x: Day
x = 40 # conversion from int literal(40) to Day is invalid

.. or at run-time

var x: Day
var y = unknownInt() # let's say it returns 100

x = y # unhandled exception: value 100 out of range [EOutOfRange]

Furthermore, distinct types can be used if even stronger type safety is required.

Sostenuto answered 30/8, 2013 at 13:49 Comment(2)
Thank you. Araq also posted on #ada where we had little discussion. I was not aware that Nimrod does it as elegantly as Ada and quite happy about it, but I believe lack of GUI bindings will keep me with Ada.Anyone
Well, Ada isn't really known for its graphics-bindings. (Though I ran across this, which indicates there are some good/easy-to-use extant packages.) But, hopefully there will be a bit of work on rectifying things and a good. open, platform independent GUI-package will be forthcoming. (I don't consider mere bindings as qualifying.)Asclepius
A
0

Further, instantiation becomes one argument simpler using

import std.traits;

/*! Instantiator for \c Limited. */
template limited(alias min, alias max, bool Exceptional = true)
    if (!is(CommonType!(typeof(min), typeof(max)) == void)) {
    auto limited(CommonType!(typeof(min), typeof(max)) value) {
        return Limited!(typeof(value), min, max)(value);
    }
}

See Instantiator Function for Bound template doesn't compile

Adverse answered 13/9, 2013 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.