Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 01, 2010 templates and scoping | ||||
---|---|---|---|---|
| ||||
this is going to be a bit of a nebulous question, but if I define a template template T(string s){ } where s is an identifier, then is there a good way to branch T's behavior based on whether s is declared in the scope in which T gets instantiated? |
April 01, 2010 Re: templates and scoping | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | Hello Ellery, > this is going to be a bit of a nebulous question, but > > if I define a template > > template T(string s){ > } > where s is an identifier, then is there a good way to branch T's > behavior based on whether s is declared in the scope in which T gets > instantiated? > some combination of "is()" "typeof" and "mixin()" should do it. //untested static if( mixin("is("~s~")") ) { ... } -- ... <IXOYE>< |
April 03, 2010 Re: templates and scoping | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | On 04/01/2010 12:38 PM, BCS wrote:
> Hello Ellery,
>
>> this is going to be a bit of a nebulous question, but
>>
>> if I define a template
>>
>> template T(string s){
>> }
>> where s is an identifier, then is there a good way to branch T's
>> behavior based on whether s is declared in the scope in which T gets
>> instantiated?
>>
>
> some combination of "is()" "typeof" and "mixin()" should do it.
>
> //untested
>
> static if( mixin("is("~s~")") ) { ... }
>
All right, here's a first try:
//// test.d
module test;
import tok;
int a; //1
void main(){
int a; //2
void dummy(){};
mixin T!("a");
}
//// tok.d
module tok;
int a; //3
template T(char[] k){
static if (is(typeof(mixin(k)))){
pragma(msg,"true");
static if(is(typeof(mixin("."~k)))){
pragma(msg,"uh oh");
}
const K = true;
}else{
pragma(msg,"false");
const K = false;
}
}
The problem I see is it can't distinguish between 1 or 2 and 3
|
April 03, 2010 Re: templates and scoping | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | Hello Ellery, > The problem I see is it can't distinguish between 1 or 2 and 3 > OK, lets try a slightly different question: Can code statically branch on what scope some symbol is in? I'd look at __traits but I don't know how to do this off hand. Being in a template mixin and getting passed in as a string just confuse the issue. -- ... <IXOYE>< |
Copyright © 1999-2021 by the D Language Foundation