Mixin templates accessing mixed out scope
Prudence via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 12 06:37:35 PDT 2015
The following code simply does not work, it might be buggy now
after fiddling with it but basically remove and the
SingleStore.New are not working(Says the creating new SingleStore
is not an expression, which makes no sense to me).
Essentially I'm creating a mixin template so I can have different
"object stores", which is just an associative array of arrays.
The SingleStore wraps the key, value pair added to the store so
that I can keep track of and remove the added object easily
without having to explicitly remember everything(e.g., what if
TValue is a delegate? Then it get's messy).
Why remove can't disambiguate is beyond me... Why I can't create
a SingleStore!(int, double)() is beyond me! It would be nice if
D's errors were a little more helpful!
Error Error: type SingleStore!(int, double) is not an
expression Test.d 56
auto o = new SingleStore!(TKey, TValue)(k, v);
huh??? Have I just lost it or is this how one is suppose to
create such an object?
(I do realize that I do not have to parameterize SingleStore. It
is irrelevant here though)
import std.stdio;
import std.concurrency;
extern (C) int getch();
import std.string;
import std.concurrency;
import core.time;
import core.thread;
import std.container.array;
import std.typecons;
// Creates a static Associative Array that stores multiple values
per key. The object returned by New can then be used to remove
the object without having to remember the object specifically.
public mixin template ObjectStore(TKey, TValue)
{
// The object store. It is static. Mixin the template into it's
different types to create different types of stores. All objects
of that type are then in the same store.
public static TValue[][TKey] store;
public static auto New(TKey k, TValue v)
{
(store[k]) ~= v;
auto o = SingleStore!(TKey, TValue).New(k, v);
return o;
}
public static auto Remove(SingleStore!(TKey, TValue) o)
{
import std.algorithm;
//remove!(c => (this.Value == c))(store[o.Key],
SwapStrategy.unstable);
}
public class SingleStore(TKey, TValue)
{
public TValue Value;
public TKey Key;
public auto Remove()
{
import std.algorithm;
//remove!("c => (this.Value == c)")(store[this.Key],
SwapStrategy.unstable);
//super.Remove(this);
}
public static auto New(TKey k, TValue v)
{
auto o = new SingleStore!(TKey, TValue)(k, v);
return o;
}
private this(TKey k, TValue v)
{
Key = k;
Value = v;
}
}
}
class MyStore
{
mixin ObjectStore!(int, double);
}
void main()
{
auto s = new MyStore();
getch();
More information about the Digitalmars-d-learn
mailing list