Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 29, 2009 define new types within templates | ||||
---|---|---|---|---|
| ||||
There was a way to define new types within templates and I think that I have seen that demonstrated here in the newsgroups, but cannot find it now. Can someone help me please? I would like to do something like this: template MyTemplate(T) { struct T ~ "Struct" // define FooStruct structure { int x; } class T ~ "Class" // define FooClass class { void bar(T ~ "Struct" t) { // ... } } } void main() { mixin MyTemplate!("Foo"); FooStruct t; FooClass f = new FooClass(); f.bar(t); } Hopefully I am not mistaken. |
December 29, 2009 Re: define new types within templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to teo | On Tue, 29 Dec 2009 17:44:18 -0500, teo <teo.ubuntu.remove@yahoo.com> wrote:
> There was a way to define new types within templates and I think that I
> have seen that demonstrated here in the newsgroups, but cannot find it
> now. Can someone help me please?
>
> I would like to do something like this:
>
> template MyTemplate(T)
> {
> struct T ~ "Struct" // define FooStruct structure
> {
> int x;
> }
> class T ~ "Class" // define FooClass class
> {
> void bar(T ~ "Struct" t)
> {
> // ...
> }
> }
> }
>
> void main()
> {
> mixin MyTemplate!("Foo");
> FooStruct t;
> FooClass f = new FooClass();
> f.bar(t);
> }
>
> Hopefully I am not mistaken.
Did you try string mixins? Maybe something like this untested code:
template MyTemplate(T)
{
mixin (CtfeReplace(q{
struct _T_Struct
{
int x;
}
}, "_T_", T.stringof));
mixin (CtfeReplace(q{
class _T_Class
{
void bar(_T_Struct t)
{
// ...
}
}
}, "_T_", T.stringof));
}
Sorry if this is a duplicate post. I attempted to cancel my earlier post.
|
December 30, 2009 Re: define new types within templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to teo | teo wrote:
> There was a way to define new types within templates and I think that I have seen that demonstrated here in the newsgroups, but cannot find it now. Can someone help me please?
>
> I would like to do something like this:
>
> template MyTemplate(T)
> {
> struct T ~ "Struct" // define FooStruct structure
> {
> int x;
> }
> class T ~ "Class" // define FooClass class
> {
> void bar(T ~ "Struct" t)
> {
> // ...
> }
> }
> }
>
> void main()
> {
> mixin MyTemplate!("Foo");
> FooStruct t;
> FooClass f = new FooClass();
> f.bar(t);
> }
>
> Hopefully I am not mistaken.
I don't know for what you're asking, but there's absolutely no need to use string mixins (see Trass3r's reply).
Instead you can just define a type inside the template, and then access it by instantiating it:
template MyTemplate(T) {
struct Struct {
T x;
}
}
void main() {
MyTemplate!(int).Struct t;
t.x = 123;
}
You can use mixin MyTemplate!(int); to bring "Struct" into the module namespace, and you can use e.g. "alias MyTemplate!(int) IntStruct;" to save typing.
|
December 30, 2009 Re: define new types within templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to teo | Hello teo,
> There was a way to define new types within templates and I think that
> I have seen that demonstrated here in the newsgroups, but cannot find
> it now. Can someone help me please?
>
> I would like to do something like this:
>
> template MyTemplate(T)
> {
> struct T ~ "Struct" // define FooStruct structure
> {
> int x;
> }
> class T ~ "Class" // define FooClass class
> {
> void bar(T ~ "Struct" t)
> {
> // ...
> }
> }
> }
> void main()
> {
> mixin MyTemplate!("Foo");
> FooStruct t;
> FooClass f = new FooClass();
> f.bar(t);
> }
> Hopefully I am not mistaken.
>
what I would do is this:
template MyTemplate(char[] name)
{
struct S { int x; }
class C { ... }
// put the minimum you can in the string mixin.
mixin ("alias S "~name~"Struc; alias C "~name~"Class;");
}
|
Copyright © 1999-2021 by the D Language Foundation