Re: Template to create a type and instantiate it

2016-02-05 Thread Marco Leise via Digitalmars-d-learn
Mixin templates is the way to go if you want something new on
every use of the template. Otherwise using the template
multiple times with the same arguments will always give you
the first instance.

-- 
Marco



Re: Template to create a type and instantiate it

2016-02-05 Thread cy via Digitalmars-d-learn

On Saturday, 6 February 2016 at 06:39:27 UTC, Marco Leise wrote:
using the template multiple times with the same arguments will 
always give you the first instance.


Hmm, consider that the argument was a particular line of code 
though, and that's not likely to repeat. I didn't test what would 
happen if you did the same code twice, though...


Re: Template to create a type and instantiate it

2016-02-05 Thread cy via Digitalmars-d-learn
On Friday, 5 February 2016 at 07:44:29 UTC, Rikki Cattermole 
wrote:

That code is completely wrong anyway.


Well, obviously it's wrong. If I don't know correct code that 
will do what I want, then I can't tell you what I want using 
correct code.



But you could do:

alias Derp = TFoo;
Derp obj;


I wasn't trying to make instances of TFoo. I was trying to make a 
type on the spot every time TFoo is used.


Sort of like std.functional.unaryFun!, which I found since 
looking around for information about it. It looks like the key to 
doing it is using "mixin" inside the template declaration itself. 
Also in realizing that "template" can be treated like its own 
source code for the purpose of mixins.


template Thing(alias code) {
class Thing {
int a, b;
this() {
mixin(code);
}
static Thing instance;
static this() {
instance = new Thing;
}
}
}
import std.stdio: writeln;

mixin Thing!q{
writeln("a ",this.a," b ",this.b);
};

int main() {
writeln("the instance exists... somewhere...");
return 0;
}

You could also say alias Q = Thing!... if accessing Q.instance is 
important.


Re: Template to create a type and instantiate it

2016-02-04 Thread Rikki Cattermole via Digitalmars-d-learn

On 05/02/16 8:41 PM, cy wrote:

I'm guessing I have to use a "mixin" mixin for this, but... there's no
way to do something like this is there?

template TFoo(T) {

struct T {
   int a;
   int b;
}

T obj;
}

TFoo!Derp;
Derp bar;

Neither templates, nor mixin templates seem capable of this. Easy enough
to use mixin, with tokenized string literal format, I think. I just
hesitate to make opaque string evaluation a thing if some more
meaningful method exists.


That code is completely wrong anyway.
But you could do:

alias Derp = TFoo;
Derp obj;

struct TFoo {
int a, b;
}

Of course you can make TFoo a template so that alias could initiate it 
with your arguments.


Template to create a type and instantiate it

2016-02-04 Thread cy via Digitalmars-d-learn
I'm guessing I have to use a "mixin" mixin for this, but... 
there's no way to do something like this is there?


template TFoo(T) {

struct T {
  int a;
  int b;
}

T obj;
}

TFoo!Derp;
Derp bar;

Neither templates, nor mixin templates seem capable of this. Easy 
enough to use mixin, with tokenized string literal format, I 
think. I just hesitate to make opaque string evaluation a thing 
if some more meaningful method exists.