"Value class instance" pattern?

Benjamin Thaut code at benjamin-thaut.de
Sun Jul 14 04:27:34 PDT 2013


Am 13.07.2013 17:15, schrieb bearophile:
> (Sorry, this post has gone in this newsgroup by mistake, but it's a
> small mistake.)
>
>
> To change and understand your code a bit (now the ComposeClass
> constructor is a template) I have removed some of the tests and debug
> code, but they should be added back later:
>
>
> /*
> string listAvailableCtors(T)() {
>      string result = "";
>      foreach(t; __traits(getOverloads, T, "__ctor"))
>          result ~= typeof(t).stringof ~ "\n";
>      return result;
> }
> */
>
> struct DefaultCtor {} //call default ctor type
> enum defaultCtor = DefaultCtor();
>
> struct ComposeClass(T) if (is(T == class)) {
>      void[__traits(classInstanceSize, T)] _classMemory = void;
>      bool m_destructed = false;
>
>      @property T _instance() {
>          return cast(T)_classMemory.ptr;
>      }
>
>      @property const(T) _instance() const {
>          return cast(const(T))_classMemory.ptr;
>      }
>
>      alias _instance this;
>
>      @disable this();
>      @disable this(this);
>
>      this(DefaultCtor) {
>          // _classMemory[] = typeid(T).init[]; // needed?
>          _instance.__ctor;
>      }
>
>      this(Targs...)(Targs args) {
>          _classMemory[] = typeid(T).init[];
>          _instance.__ctor(args);
>      }
>
>      ~this() {
>          if (!m_destructed) {
>              _instance.destroy;
>              m_destructed = true;
>          }
>      }
> }
>
> // Usage example ----------
>
> class Foo {
>      int i, j;
>      this() {
>          this.i = 5;
>      }
>
>      this(int ii) {
>          this.i = ii;
>      }
> }
>
> class Bar {
>      ComposeClass!Foo f;
>
>      this() {
>          //f = typeof(f)(defaultCtor);
>          f = typeof(f)(2); // alternative
>      }
> }
>
> void main() {
>      import std.stdio;
>      auto bar = new Bar;
>      writeln(bar.f.i);
>      bar.f.i = 1;
>      writeln(bar.f.i);
> }
>
>
> This code is unfinished.
> Is the assignment needed in this(DefaultCtor)?
>
> This code contains some sharp edges (for the D programmer and even for
> the compiler, I have opened a new bug report:
> http://d.puremagic.com/issues/show_bug.cgi?id=10629 ), for me it's easy
> to get wrong, hard to get right & good, and I believe it's of general
> usefulness, so I think it's fit for Phobos.
>
> But isn't it a replacement for Phobos Scoped?
>
> Bye,
> bearophile

I just noticed that this still does not work. Even with dmd 2.063 I get 
the error message:

main.d(28): Error: template 
main.ComposeClass!(Object).ComposeClass.__ctor(Targs...)(Targs args) 
conflicts with constructor main.ComposeClass!(Object).ComposeClass.this 
at main.d(20)

If you have a templated constructor you can't have any non templated 
constructors. (http://d.puremagic.com/issues/show_bug.cgi?id=4749)
So a version with a templated constructor is still not doable at the moment.

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list