June 23, 2006 Re: static if check for array and AA | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Kirk McDonald wrote:
>> I'm fairly certain I've seen this come up before, but I can't seem to rediscover the thread.
>>
>> I'm trying to write a static if condition to check whether a certain type is an array, and another check for an associative array. The former proved to be moderately simple:
>>
>> bool isArray(T) () {
>> static if (is(typeof(T.init[0])[] == T)) {
>> return true;
>> } else {
>> return false;
>> }
>> }
>>
>> Note that this only checks for dynamic arrays.
>>
>> Associative arrays are harder. The above trick can't quite work as the type of the key can't be relied upon. It's easy enough to do the check using RTTI:
>>
>> bool isAA(T) () {
>> TypeInfo t = typeid(T);
>> if (cast(TypeInfo_AssociativeArray) t) {
>> return true;
>> } else {
>> return false;
>> }
>> }
>>
>> But of course that is a runtime check, and this should really be a compile-time check. (Also, the various subclasses of TypeInfo are undocumented, so I'm not sure if it's a good idea to rely on them.)
>>
>> Anyone else have some template judo to share?
>>
>> -Kirk McDonald
>
>
> template isAA(T)
> {
> const isAA = is((cast(T)null).keys);
> }
>
> ???
>
> or something like that
template isAA(T)
{
const bool isAA = T.mangleof[0]=='H';
}
template isStaticArray(T)
{
const bool isStaticArray = T.mangleof[0]=='G';
}
template isDynamicArray(T)
{
const bool isDynamicArray = T.mangleof[0]=='A';
}
You can check for ANYTHING in this way.
Of course this relies on the name mangling algorithm which is not yet stabilised and documented -- but which should be, by DMD 1.0.
|
June 23, 2006 Re: static if check for array and AA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Don Clugston wrote:
> Of course this relies on the name mangling algorithm which is not yet stabilised and documented -- but which should be, by DMD 1.0.
Maybe, but it should it be standardized as part of D? (I hesitate to say yes.)
|
June 24, 2006 Re: static if check for array and AA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote: > Oskar Linde wrote: >> In article <e7djp7$42g$1@digitaldaemon.com>, Kirk McDonald says... >> >>> It turns out the above template fails because "is(typeof(T.init[0]))" fails, as it doesn't describe a valid type. (You can't subscript an int!) >> >> Yes. I've been reporting this a couple of time, but never gotten any (official) >> response on whether this is the intended behavior. Static arrays are the only type for which typeof(T.init) >> != T. > > This is also somewhat close to implementation-specific behavior. I'd prefer to have a more formal means of detecting static arrays. > > > Sean I'm not sure that is implementation-specific behavior. I suspect that that is related to the fact that static arrays are the only type that cannot be assigned to a value of the same type (sar1 = sar2). So in order for something like this: T sar = T.init ; to work for all types, then T.init had to an element of the static array. I think that all of this (arrays not being proper value types) is pretty inconsistent, although most of us are so used to C and C++ that we don't even notice or are bothered by it -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D |
June 26, 2006 Re: static if check for array and AA | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Don Clugston wrote:
>> Of course this relies on the name mangling algorithm which is not yet stabilised and documented -- but which should be, by DMD 1.0.
>
> Maybe, but it should it be standardized as part of D? (I hesitate to say yes.)
It should definitely be standardised. Failure to standardise the name mangling and the ABI was one of the major mistakes of C++. It makes linking between different vendors virtually impossible (and it's why so many more libraries use C linking than C++).
|
Copyright © 1999-2021 by the D Language Foundation