Re: How to check for instantiation of specific template?

2013-10-11 Thread Jacob Carlborg

On 2013-10-10 19:23, H. S. Teoh wrote:

I have a template used for storing compile-time values:

template Def(int x, string y) {
alias impl = TypeTuple!(x,y);
}

How do I define a template isDef that, given some template alias A,
evaluates to true if A is some instantiation of Def?

template isDef(alias A) {
enum A = ... /* what to put here? */
}

The intent is to be able to write signature constraints like this:

auto myFunc(alias def)(...)
if (isDef!def)
{
...
}

...
// Pass an instantiation of Def to myFunc
auto x = myFunc!(Def!(1, abc))(args);

I tried using std.traits.isInstanceOf but apparently it expects the
second argument to be an actual type, which doesn't work in this case
because Def is a typetuple of compile-time values, not an actual type.


I found this old thread

http://forum.dlang.org/thread/mailman.1382.1371938670.13711.digitalmar...@puremagic.com

With this code:

template getTemplate(alias T : TI!TP, alias TI, TP...)
{
alias getTemplate = TI;
}

But I haven't figured out how to do the comparison.

--
/Jacob Carlborg


Re: How to check for instantiation of specific template?

2013-10-11 Thread Dicebot

This is actually a very interesting question.

Usual approach is to use pattern matching on types but template 
instance symbol is not a type on its own, it is just a symbol. 
(with typeof == void)


My second guess was to check __traits(identifier, AliasParam) but 
it looks like for aliased instance (contrary to aliased template 
symbol) identifier is its mangled name.


I can't imagine really clean way to do it but comparing 
__traits(identifier) to that specific mangled name can be a 
reliable enough hack (better than stringof which has undefined 
format).


How to check for instantiation of specific template?

2013-10-10 Thread H. S. Teoh
I have a template used for storing compile-time values:

template Def(int x, string y) {
alias impl = TypeTuple!(x,y);
}

How do I define a template isDef that, given some template alias A,
evaluates to true if A is some instantiation of Def?

template isDef(alias A) {
enum A = ... /* what to put here? */
}

The intent is to be able to write signature constraints like this:

auto myFunc(alias def)(...)
if (isDef!def)
{
...
}

...
// Pass an instantiation of Def to myFunc
auto x = myFunc!(Def!(1, abc))(args);

I tried using std.traits.isInstanceOf but apparently it expects the
second argument to be an actual type, which doesn't work in this case
because Def is a typetuple of compile-time values, not an actual type.


T

-- 
Help a man when he is in trouble and he will remember you when he is in trouble 
again.


Re: How to check for instantiation of specific template?

2013-10-10 Thread Simen Kjaeraas

On 2013-10-10, 19:23, H. S. Teoh wrote:


I have a template used for storing compile-time values:

template Def(int x, string y) {
alias impl = TypeTuple!(x,y);
}

How do I define a template isDef that, given some template alias A,
evaluates to true if A is some instantiation of Def?

template isDef(alias A) {
enum A = ... /* what to put here? */
}


This seems to be impossible. The standard way of doing this would be
with an isExpression[0], but that only works for types.

A solution would be to beef up your Def template by turning it into
a struct:

struct Def(int x, string y) {
alias impl = TypeTuple!(x,y);
}

With this definition, the following works:

template isDef(alias A) {
enum isDef = is(A == Def!T, T...);
// Or enum isDef = is(A == Def!(x, y), int x, string y);
}

This is a shortcoming of the language, and I have filed an enhancement
request for it[1].

The problems of using an actual type for this, is that someone might
instantiate that type (unlikely), and that the compiler will generate
TypeInfo for it, which will bloat your executable. A Sufficienctly
Smart Compiler[2] would see that the type is never used and thus not
include the TypeInfo. Something which the compiler could do without
a lot of smarts, is optimize away final abstract classes, which you
cannot derive from and cannot instantiate. That oughta be enough.

[0]: http://dlang.org/expression#IsExpression
[1]: http://d.puremagic.com/issues/show_bug.cgi?id=11219
[2]: http://c2.com/cgi/wiki?SufficientlySmartCompiler
--
Simen


Re: How to check for instantiation of specific template?

2013-10-10 Thread simendsjo

On Thursday, 10 October 2013 at 17:24:37 UTC, H. S. Teoh wrote:

I have a template used for storing compile-time values:

template Def(int x, string y) {
alias impl = TypeTuple!(x,y);
}

How do I define a template isDef that, given some template 
alias A,

evaluates to true if A is some instantiation of Def?

template isDef(alias A) {
enum A = ... /* what to put here? */
}

The intent is to be able to write signature constraints like 
this:


auto myFunc(alias def)(...)
if (isDef!def)
{
...
}

...
// Pass an instantiation of Def to myFunc
auto x = myFunc!(Def!(1, abc))(args);

I tried using std.traits.isInstanceOf but apparently it expects 
the
second argument to be an actual type, which doesn't work in 
this case
because Def is a typetuple of compile-time values, not an 
actual type.



T


I've used this horrible hack to solve this. Not very generic as 
you can see:
else static if(T.stringof.length  13  T.stringof[0..13] == 
TupleWrapper!)