Thread overview
Handle to some object, call its methods
Jul 15, 2014
Anonymous
Jul 15, 2014
Ali Çehreli
Jul 16, 2014
Anonymous
July 15, 2014
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.
July 15, 2014
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

July 16, 2014
On Tuesday, 15 July 2014 at 17:06:14 UTC, Ali Çehreli wrote:
> 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

Thanks, that adapted well.