== Auszug aus Steven Schveighoffer (schvei...@yahoo.com)'s Artikel > On Mon, 16 May 2011 16:12:05 -0400, nrgyzer <nrgy...@gmail.com> wrote: > > == Auszug aus Steven Schveighoffer (schvei...@yahoo.com)'s Artikel > >> On Mon, 16 May 2011 15:32:43 -0400, useo <unkn...@unknown.com> > > wrote: > >> > Hey guys, > >> > > >> > is there any chance to create an abstract constructor like: > >> > > >> > abstract class ABC { > >> > > >> > abstract this(); > >> > > >> > } > >> > > >> > DMD always says "...this non-virtual functions cannot be > > abstract" - > >> > when I use an interface like: > >> > > >> > interface ABC { > >> > > >> > this(); > >> > > >> > } > >> > > >> > I get a similar error: "...constructors, destructors, postblits, > >> > invariants, unittests, new and delete functions are not allowed in > >> > interface ABC" > >> > > >> > Is there any solution or is it possible to create such > > inheritances > >> > in DMD? > >> I think what you are trying to do is say, "if a class implements > > interface > >> ABC, it must have a default constructor". Such a requirement is > > faulty. > >> The point of an interface is to able to pass a portion of a class' > >> functionality to a function during runtime. However, the instance > > must > >> *already exist*. It makes no sense to posit requirements on the > >> constructor. > >> What you want is a compile-time requirement using a template > > constraint. > >> You may think "damn, but I don't want to make my function a > > template", I'd > >> say see previous point ;) > >> -Steve > > > > Okay, thanks... perhaps someone know a better solution: I have one > > big file which contains some other files (let's say: blocks). Each > > block has it's own signature... by reading the big file, I read the > > signature of each block. Based on the signature, I read block A, > > block B or another Block. To do that, I want call the block- specific > > constructor which reads the next bytes. > > > > !Semicode: > > > > ... > > ABC[] blocks; > > ... > > while (!eof(bigfile)) { > > read(signature); > > if (signature==A) blocks ~= new A(bigfile); > > else if (signature==B) blocks ~= new B(bigfile); > > ... > > } > > ... > No special requirements are necessary. How would this compile if A or B > did not have a bigfile constructor? The interface specification is not > needed. > If D supported runtime reflection (and it does to a very very small > degree), then you could use it to ensure the correct constructor is > available. > -Steve
It's semicode, so I haven't try to implement it... it should only show what I'm trying to do. I'm currently thinking about an empty constructor in an abstract class like: abstract class ABC { this(Stream) { // do nothing } } class A : ABC { this(Stream s) { super(s); // read my block-specific bytes } }