On Wednesday, 4 June 2014 at 18:46:04 UTC, Mason McGill wrote:
  /* User code. */
  import std.awesome_unit_library: Unit, of;
  // Unit: defines new types that can be multiplied & divided.
// of: wraps a container in a stricter version, enforcing units.

  alias Name = Unit!"name";
  alias Food = Unit!"food";
  auto names = ["Fred", "Alice", "Sue"].of!Name;
  auto foods = ["Apple", "Orange", "Tofu"].of!Food;

  foreach (name; ["Bill", "Ted"].of!Name)
  {
      names ~= name; // This compiles happily.
      foods ~= name; // This does not compile, preventing
                     // Bill and Ted from being eaten.
  }

You could actually just let the `of` template create the `Unit` type for you, if you want to be terse:

  import std.awesome_unit_library: of;

  auto names = ["Fred", "Alice", "Sue"].of!"name";
  auto foods = ["Apple", "Orange", "Tofu"].of!"food";

  foreach (name; ["Bill", "Ted"].of!"name")
  {
      names ~= name; // This compiles happily.
      foods ~= name; // This does not compile, preventing
                     // Bill and Ted from being eaten.
  }

It looks like this library solution puts user work at or below the level of your proposed syntax, though I'll have to think about whether it covers all the cases you're interested in.

Reply via email to