www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Handle to some object, call its methods

reply "Anonymous" <a anonymous.com> writes:
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.
Jul 15 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jul 15 2014
parent "Anonymous" <a anonymous.com> writes:
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.
Jul 15 2014