digitalmars.D.learn - how to use opdot
- Morusaka (5/5) Nov 16 2008 Hi,
- Hoenir (6/12) Nov 19 2008 Well, if D wasn't OO, I'd say it's some way to provide exactly that,
- Kagamin (2/3) Nov 20 2008 it will be useful when implementing auto_ptr, shared_ptr and likes.
- Steven Schveighoffer (27/33) Nov 20 2008 opDot is useful if you want to make a 'wrapper' type. That is, you want...
- Ary Borenszweig (3/34) Nov 20 2008 Wow. That's incredibly useful for doing decorators!
- Steven Schveighoffer (7/41) Nov 20 2008 Not exactly ;) The wrapped type is not equivalent to inheritance.
- Ary Borenszweig (2/49) Nov 20 2008
- Christopher Wright (6/7) Nov 20 2008 No, exactly:
- Steven Schveighoffer (6/13) Nov 20 2008 class Wrapper(T) : T
- Christopher Wright (4/21) Nov 20 2008 True, but with opDot, you can swap out the real value. I can think of
- Ary Borenszweig (16/38) Nov 21 2008 I'm not sure that's the decorator pattern any more. In that pattern you
- Steven Schveighoffer (4/42) Nov 21 2008 Exactly what I was going to say ;) If you inherit from a T, then return...
- Morusaka (3/39) Nov 20 2008 Thank you very much Steven!
Hi, I've read about opdot in D language spec operator overload section, but the little snippet of code provided isn't enough, for me, to figure out what it is supposed to do and how to use it or what it could be usefull for. Could you please help me to get the right way? Thank you, Luca.
Nov 16 2008
Morusaka schrieb:Hi, I've read about opdot in D language spec operator overload section, but the little snippet of code provided isn't enough, for me, to figure out what it is supposed to do and how to use it or what it could be usefull for. Could you please help me to get the right way?Well, if D wasn't OO, I'd say it's some way to provide exactly that, e.g. the scripting language Lua does it that way: if it can't find a member in a table, it looks in another one specified similarly to this opDot way. That provides some kind of "inheritance". But I can't currently imagine any way to use this either.
Nov 19 2008
Hoenir Wrote:But I can't currently imagine any way to use this either.it will be useful when implementing auto_ptr, shared_ptr and likes.
Nov 20 2008
"Morusaka" wroteHi, I've read about opdot in D language spec operator overload section, but the little snippet of code provided isn't enough, for me, to figure out what it is supposed to do and how to use it or what it could be usefull for. Could you please help me to get the right way?opDot is useful if you want to make a 'wrapper' type. That is, you want to mimic another type, but you want to slightly alter the behavior. opDot allows you to 'inherit' all the member functions and fields from the wrapped type. For example, if I wanted to create a wrapper type that added a 'blahblah' integer to the type, I could do this: struct AddBlahBlah(T) { T _t; int blahblah; T *opDot() { return &_t;} } Now, if I declare an AddBlahBlah!(C) and class C has a member foo(): C c; AddBlahBlah!(C) abb = AddBlahBlah!(C)(c); abb.foo(); // translates to abb.opDot().foo() abb.blahblah = 5; // sets abb.blahblah to 5, doesn't affect _t The goal of opDot is to allow one to create types that wrap other types that look almost exactly the same without much effort. For example, the std.typecons.Rebindable type allows one to create a rebindable const or invariant class reference while forwarding all member accesses to the underlying invariant or const instance. This feature is used for extending the type system without having to extend the language, allowing compiler enforcement of specific design aspects without defining them in the compiler. Normal developers will most likely never need to define opDot. -Steve
Nov 20 2008
Steven Schveighoffer wrote:"Morusaka" wroteWow. That's incredibly useful for doing decorators! http://en.wikipedia.org/wiki/Decorator_patternHi, I've read about opdot in D language spec operator overload section, but the little snippet of code provided isn't enough, for me, to figure out what it is supposed to do and how to use it or what it could be usefull for. Could you please help me to get the right way?opDot is useful if you want to make a 'wrapper' type. That is, you want to mimic another type, but you want to slightly alter the behavior. opDot allows you to 'inherit' all the member functions and fields from the wrapped type. For example, if I wanted to create a wrapper type that added a 'blahblah' integer to the type, I could do this: struct AddBlahBlah(T) { T _t; int blahblah; T *opDot() { return &_t;} } Now, if I declare an AddBlahBlah!(C) and class C has a member foo(): C c; AddBlahBlah!(C) abb = AddBlahBlah!(C)(c); abb.foo(); // translates to abb.opDot().foo() abb.blahblah = 5; // sets abb.blahblah to 5, doesn't affect _t
Nov 20 2008
"Ary Borenszweig" wroteSteven Schveighoffer wrote:Not exactly ;) The wrapped type is not equivalent to inheritance. For example, if you have a function that takes a class C, you can't pass an AddBlahBlah!(C) type into it. However, a template function which expects a type C or a wrapped C, could possibly be used as you say. -Steve"Morusaka" wroteWow. That's incredibly useful for doing decorators! http://en.wikipedia.org/wiki/Decorator_patternHi, I've read about opdot in D language spec operator overload section, but the little snippet of code provided isn't enough, for me, to figure out what it is supposed to do and how to use it or what it could be usefull for. Could you please help me to get the right way?opDot is useful if you want to make a 'wrapper' type. That is, you want to mimic another type, but you want to slightly alter the behavior. opDot allows you to 'inherit' all the member functions and fields from the wrapped type. For example, if I wanted to create a wrapper type that added a 'blahblah' integer to the type, I could do this: struct AddBlahBlah(T) { T _t; int blahblah; T *opDot() { return &_t;} } Now, if I declare an AddBlahBlah!(C) and class C has a member foo(): C c; AddBlahBlah!(C) abb = AddBlahBlah!(C)(c); abb.foo(); // translates to abb.opDot().foo() abb.blahblah = 5; // sets abb.blahblah to 5, doesn't affect _t
Nov 20 2008
Steven Schveighoffer wrote:"Ary Borenszweig" wroteAh, right. I forgot the inheritance part. :(Steven Schveighoffer wrote:Not exactly ;) The wrapped type is not equivalent to inheritance."Morusaka" wroteWow. That's incredibly useful for doing decorators! http://en.wikipedia.org/wiki/Decorator_patternHi, I've read about opdot in D language spec operator overload section, but the little snippet of code provided isn't enough, for me, to figure out what it is supposed to do and how to use it or what it could be usefull for. Could you please help me to get the right way?opDot is useful if you want to make a 'wrapper' type. That is, you want to mimic another type, but you want to slightly alter the behavior. opDot allows you to 'inherit' all the member functions and fields from the wrapped type. For example, if I wanted to create a wrapper type that added a 'blahblah' integer to the type, I could do this: struct AddBlahBlah(T) { T _t; int blahblah; T *opDot() { return &_t;} } Now, if I declare an AddBlahBlah!(C) and class C has a member foo(): C c; AddBlahBlah!(C) abb = AddBlahBlah!(C)(c); abb.foo(); // translates to abb.opDot().foo() abb.blahblah = 5; // sets abb.blahblah to 5, doesn't affect _tFor example, if you have a function that takes a class C, you can't pass an AddBlahBlah!(C) type into it. However, a template function which expects a type C or a wrapped C, could possibly be used as you say. -Steve
Nov 20 2008
Steven Schveighoffer wrote:Not exactly ;) The wrapped type is not equivalent to inheritance.No, exactly: class Wrapper(T) : T { T opDot() {} }
Nov 20 2008
"Christopher Wright" wroteSteven Schveighoffer wrote:class Wrapper(T) : T { } works just as good ;) -SteveNot exactly ;) The wrapped type is not equivalent to inheritance.No, exactly: class Wrapper(T) : T { T opDot() {} }
Nov 20 2008
Steven Schveighoffer wrote:"Christopher Wright" wroteTrue, but with opDot, you can swap out the real value. I can think of cases in which this would be useful -- a sort of "I'll fill in this value later" thing.Steven Schveighoffer wrote:class Wrapper(T) : T { } works just as good ;) -SteveNot exactly ;) The wrapped type is not equivalent to inheritance.No, exactly: class Wrapper(T) : T { T opDot() {} }
Nov 20 2008
Christopher Wright escribió:Steven Schveighoffer wrote:I'm not sure that's the decorator pattern any more. In that pattern you implement or extend a class, and receive an instance of one in the constructor and forward all calls to that instance. If you do: class Wrapper(T) : T { private wrapped; this(T wrapped) { this.wrapped = wrapped; } T opDot() { return wrapped; } } that won't work because whenever you call a method of T on Wrapper(T), that will call Wrapper's method, since it has it, because it extends/implements T (opDot won't be triggered). If you remove inheritance, that will probably work, but you won't be able to make a Wrapper(T) behave like a T for the type system."Christopher Wright" wroteTrue, but with opDot, you can swap out the real value. I can think of cases in which this would be useful -- a sort of "I'll fill in this value later" thing.Steven Schveighoffer wrote:class Wrapper(T) : T { } works just as good ;) -SteveNot exactly ;) The wrapped type is not equivalent to inheritance.No, exactly: class Wrapper(T) : T { T opDot() {} }
Nov 21 2008
"Ary Borenszweig" wroteChristopher Wright escribió:Exactly what I was going to say ;) If you inherit from a T, then return T in an opDot, opDot will never be called unless you call opDot directly. -SteveSteven Schveighoffer wrote:I'm not sure that's the decorator pattern any more. In that pattern you implement or extend a class, and receive an instance of one in the constructor and forward all calls to that instance. If you do: class Wrapper(T) : T { private wrapped; this(T wrapped) { this.wrapped = wrapped; } T opDot() { return wrapped; } } that won't work because whenever you call a method of T on Wrapper(T), that will call Wrapper's method, since it has it, because it extends/implements T (opDot won't be triggered). If you remove inheritance, that will probably work, but you won't be able to make a Wrapper(T) behave like a T for the type system."Christopher Wright" wroteTrue, but with opDot, you can swap out the real value. I can think of cases in which this would be useful -- a sort of "I'll fill in this value later" thing.Steven Schveighoffer wrote:class Wrapper(T) : T { } works just as good ;) -SteveNot exactly ;) The wrapped type is not equivalent to inheritance.No, exactly: class Wrapper(T) : T { T opDot() {} }
Nov 21 2008
Steven Schveighoffer Wrote:opDot is useful if you want to make a 'wrapper' type. That is, you want to mimic another type, but you want to slightly alter the behavior. opDot allows you to 'inherit' all the member functions and fields from the wrapped type. For example, if I wanted to create a wrapper type that added a 'blahblah' integer to the type, I could do this: struct AddBlahBlah(T) { T _t; int blahblah; T *opDot() { return &_t;} } Now, if I declare an AddBlahBlah!(C) and class C has a member foo(): C c; AddBlahBlah!(C) abb = AddBlahBlah!(C)(c); abb.foo(); // translates to abb.opDot().foo() abb.blahblah = 5; // sets abb.blahblah to 5, doesn't affect _t The goal of opDot is to allow one to create types that wrap other types that look almost exactly the same without much effort. For example, the std.typecons.Rebindable type allows one to create a rebindable const or invariant class reference while forwarding all member accesses to the underlying invariant or const instance. This feature is used for extending the type system without having to extend the language, allowing compiler enforcement of specific design aspects without defining them in the compiler. Normal developers will most likely never need to define opDot. -SteveThank you very much Steven! Luca
Nov 20 2008