Hacker News new | ask | show | jobs
by WhereIsTheTruth 700 days ago
You can in D

    struct Foo
    {
        void read(){}
    }

    struct Bar
    {
        void read(){}
    }

    struct Nope
    {

    }

    void handle(T)(T data)
    {
        static if (__traits(hasMember, data, "read") == false) static assert(0, "struct needs a 'read' function");
        data.read();
    }

    void main()
    {
        Foo foo;
        Bar bar;
        Nope nope;

        handle(foo); // works
        handle(bar); // works
        handle(nope); // oops main.d(17): Error: static assert:  "struct needs a 'read' function"
    }