On 07/15/2014 09:39 AM, Anonymous wrote:

> struct Subscription {
>      const Object handle;
>      private immutable size_t index;
>      @disable this();
>      private this(Object o, size_t i) {
>          handle = o;
>          index = i;
>      }
> }
>
> I'd like this to be constructed with a handle to some object, and some
> other details. The source would create and return the Subscriptions, and
> the reader would call the object's const methods through the handle. The
> const "read" methods would take the Subscription as one of the arguments.

Object is too general for that. Normally, you would define an interface and use that:

interface Reader
{
    int read() const;
}

struct Subscription {
     const Reader reader;
     private immutable size_t index;
     @disable this();
     private this(Reader r, size_t i) {
         reader = r;
         index = i;
     }
}

class ConstantReader(int value) : Reader
{
    int read() const
    {
        return value;
    }
}

void main()
{
    auto sub = Subscription(new ConstantReader!42(), 0);
    assert(sub.reader.read() == 42);
}

Further, if 'index' is the subscription index, usually there is no need for that as the Subscription objects can be iterated over in their container:

    Subscription[] subs;
    subs ~= Subscription(new ConstantReader!42(), 0);

    foreach (sub; subs) {
        sub.reader.read();
    }

Ali

Reply via email to