[Issue 2179] import inside class works but is not in spec

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jul 14 08:16:47 PDT 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2179





------- Comment #3 from aarti at interia.pl  2008-07-14 10:16 -------
(In reply to comment #2)

> It is pretty interesting, but it would be vastly more so if import would work
> inside a template 

It works right now, but as Bill noticed is not in specs. See:
http://www.dsource.org/projects/doost/browser/trunk/doost/util/serializer/archive/JsonArchive.d

> and were parameterized with the parameters of the template,
> or in general if the import could be somehow parameterized. 

AFAIK according spec template alias parameter can be import also. And maybe
mixin("import std.stdio;") will work also... Though I did not test it.

> As it is, I'm not
> seeing many good applications.

With imports in templates it is possible to separate specific implementation
from general code. So I would not say that it is not useful :-)

> For example, say I want to define a module "cloneable" so I can say:
>
> class A { import cloneable; }
> class B : A { import cloneable; }
> auto x = new A;
> auto y = x.clone;

Although below already works:

class A { mixin cloneable; }
class B : A { mixin cloneable; }

I used it in serializer, which is parametrized with archive class, and then
proper class body is imported into base class.

See: 
http://www.dsource.org/projects/doost/browser/trunk/doost/util/serializer/Serializer.d

It works nice, but there is still a lot of rough edges and developing
serializer was sometimes really painful. E.g. I can not remove now (in 1.0
branch) spurious parameter to template archive. Program compiles with this
argument, but not without it, although template argument (ARCHIVE) is not used
anywhere.

> and so on. But in the definition of clone (inside the cloneable module) we'd
> need the type of the class under construction. Guess that's where typeof(this)
> would come in handy :o).

I don't get this part. For me some clarifications would be helpful, but maybe
its just me :-)


-- 



More information about the Digitalmars-d-bugs mailing list