digitalmars.D.learn - Handle to some object, call its methods
- Anonymous (14/14) Jul 15 2014 struct Subscription {
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (37/50) Jul 15 2014 Object is too general for that. Normally, you would define an interface
- Anonymous (2/57) Jul 15 2014 Thanks, that adapted well.
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
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
On Tuesday, 15 July 2014 at 17:06:14 UTC, Ali Çehreli wrote:On 07/15/2014 09:39 AM, Anonymous wrote:Thanks, that adapted well.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 someother details. The source would create and return theSubscriptions, andthe reader would call the object's const methods through thehandle. Theconst "read" methods would take the Subscription as one ofthe 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