[GSoC Proposal] Statically Checked Measurement Units

Andrej Mitrovic andrej.mitrovich at gmail.com
Wed Mar 30 07:49:10 PDT 2011


Maybe OT, but here's some hackish wizardry you can do with classes:

class Test
{
    int value;

    this(int x)
    {
        value = x;
    }
}

ref Test foo(Test t ...)
{
    return tuple(t)[0];
}

void main()
{
    auto result = foo(4);
    assert(result.value == 4);
}

The Tuple is used to trick DMD into escaping the local `t` reference.
This won't work with structs. And `t` should be constructed on the
stack, but it seems the destructor gets called only after the exit
from main. The docs do say that construction of classes in variadic
arguments depend on the implementation.

I asked on the newsgroups whether Typesafe Variadic Functions
automatically calling a constructor was a good thing at all. Read
about this feature here under "Typesafe Variadic Functions":
http://www.digitalmars.com/d/2.0/function.html


More information about the Digitalmars-d mailing list