digitalmars.D.learn - Interface abstraction
- Frustrated (8/8) Dec 31 2013 If I use interfaces instead of classes is there a way to have new
- Adam D. Ruppe (3/6) Dec 31 2013 Right, it'll always be usable anyway.
- Frustrated (6/12) Dec 31 2013 I guess I was thinking just for consistency since I'll always
- Adam D. Ruppe (8/11) Dec 31 2013 Yeah, if you use your own factory methods, you can always just
If I use interfaces instead of classes is there a way to have new return the underlying interface that I want it to? interface A { } class B : A {} auto a = new B; // should return cast(A)(new B); Maybe it's not such a big deal though? (I can't think of any case where a being of type B will hurt since it is always implicitly castable to type A.
Dec 31 2013
On Wednesday, 1 January 2014 at 00:31:03 UTC, Frustrated wrote:auto a = new B; // should return cast(A)(new B);A a = new B;case where a being of type B will hurt since it is always implicitly castable to type A.Right, it'll always be usable anyway.
Dec 31 2013
On Wednesday, 1 January 2014 at 00:31:58 UTC, Adam D. Ruppe wrote:On Wednesday, 1 January 2014 at 00:31:03 UTC, Frustrated wrote:I guess I was thinking just for consistency since I'll always being trying to use interfaces instead of classes. I guess it's not a big deal though. In setting up little test units I'll end up using the classes and auto but probably not in the full project where I'll use factories and such.auto a = new B; // should return cast(A)(new B);A a = new B;case where a being of type B will hurt since it is always implicitly castable to type A.Right, it'll always be usable anyway.
Dec 31 2013
On Wednesday, 1 January 2014 at 00:54:35 UTC, Frustrated wrote:not a big deal though. In setting up little test units I'll end up using the classes and auto but probably not in the full project where I'll use factories and such.Yeah, if you use your own factory methods, you can always just have them return the interface. Even as simple as: InterfaceName make(Class, Args...)(Args args) { return new Class(args); } which explicitly returns the interface, so if you did auto a = make!Foo(10, 20); // a is typed to the interface
Dec 31 2013