template+alias as an object's "namespace"
kennytm
kennytm at gmail.com
Wed Apr 18 15:40:55 PDT 2012
"F i L" <witte2008 at gmail.com> wrote:
> Would it be prudent to make Templates be able to be used without
> parameters? (Or something along those lines) Consider the following:
>
> struct Foo1 {
> string name = "Foo1";
>
> alias Unsafe!() unsafe;
>
> template Unsafe() {
> string name = "Bar";
>
> void bar() {
> writeln(this.name, name);
> }
> }
> }
>
> struct Foo2 {
> string name = "Foo2";
> string unsafe_name = "Bar";
>
> void unsafe_bar() {
> writeln(name, unsafe_name);
> }
> }
>
> void main() {
> auto foo1 = Foo1();
> auto foo2 = Foo2();
>
> foo1.Unsafe!().bar(); // ugly
> foo1.unsafe.bar(); // nice
> foo2.unsafe_bar(); // classic
> }
>
> The classic way works, or course, but it's not as pretty :)
> I may be breaking some sacred rule of OOP here, but I think this kind of
> classification comes in handy. In fact, I think D's the only language
> that actually allows for this syntax without increasing an objects memory footprints.
>
> It could be *useful* in situations where you want to simplistically wrap
> a complex system, say X11, and provide _a few_ convenience functions,
> while still providing complete access to X11 objects, but grouping them
> into logical subsection. Example:
>
> class Window {
> private XDisplay* _display;
>
> void create(...) { /* use _display; */ }
> void delete(...) { /* ditto */ }
>
> template X11 {
> @property auto display() { return _display; }
> }
> }
>
> The only thing that erks me is the name "template" doesn't really match
> it's function at this point. Maybe something like "alias template" would make more sense:
>
> alias template X11 { ... }
>
> idk. What are peoples thoughts on this? Good? Bad? Ugly?
struct Foo1 {
struct Unsafe {
static:
void bar() { ... }
}
}
Foo1 f;
f.Unsafe.bar();
More information about the Digitalmars-d
mailing list