Neat: UFCS for integer dot operator suffix

monarch_dodra monarchdodra at gmail.com
Mon Sep 24 01:09:36 PDT 2012


I tried this, and found it neat that it works:

----
import std.traits;

@property @safe pure nothrow
{
     T Mile(T)(T i)
         if (isNumeric!T)
     {
         return i*1609.44;
     }

     T Ki(T)(T i)
         if (isNumeric!T)
     {
         return i*1024;
     }

     T m(T)(T i)
         if (isNumeric!T)
     {
         return i/1000;
     }
}

void main()
{
     auto medication = 60.m;
     int[32.Ki] buffer;
     auto parisToBerlib = 544.93.Mile;
}

----

It is not homogenous (eg, Km vs Kg vs Kv), but it makes writing 
unit-less sizes pretty easy.

This has probably been discovered before, but it is new to me.

Of course, nothing that can't be done with a simple multiply, but 
I found there is something more... "special"... to doing it with 
dot.


More information about the Digitalmars-d mailing list