Thread overview
How to extract the AA type?
Dec 13, 2017
jmh530
Dec 13, 2017
Jack Applegame
December 13, 2017
A nice puzzle for those template gurus out there.

I have a function like this:

auto foo(T: V[K], V, K)(T t)
{
   RealAAType!(T, V, K) aa = t;
   return aa;
}

So I need to know what to write RealAAType. What I'm looking for is a mechanism to write the exact AA type that is passed in. RealAAType should be an AA, and nothing else.

Here are 2 things that *don't* work:

alias RealAAType!(T, V, K) = T;

This doesn't work because T could be an alias this'd struct. I don't want T, I want the actual AA type.

alias RealAAType!(T, V, K) = V[K];

This doesn't work when the T has a type modifier. For instance if T is const(int[int]), then V[K] is really const(int)[int], and you can't assign it to t.

Here is are the tests I want to compile:

const(int[int]) x;
auto aa1 = foo(x);
static assert(typeof(aa1) == const(int[int]));

static struct S
{
   int[int] aa;
   alias aa this;
}

const(S) s;
auto aa2 = foo(s);
static assert(typeof(aa2) == const(int[int]));

-Steve
December 13, 2017
On Wednesday, 13 December 2017 at 16:00:32 UTC, Steven Schveighoffer wrote:
> A nice puzzle for those template gurus out there.
>
> I have a function like this:
>
> auto foo(T: V[K], V, K)(T t)
> {
>    RealAAType!(T, V, K) aa = t;
>    return aa;
> }
>
> So I need to know what to write RealAAType. What I'm looking for is a mechanism to write the exact AA type that is passed in. RealAAType should be an AA, and nothing else.
>
[snip]

How about:

template RealAAType!(T, V, K)
{
    import std.traits : CopyConstness;

    alias RealAAType = CopyConstness!(T, V[K]);
}
December 13, 2017
auto foo(T: V[K], V, K)(T t)
{
    CopyConstness!(T, V[K]) aa = t;
    return aa;
}

https://run.dlang.io/is/LSMa5C
December 13, 2017
On 12/13/17 11:30 AM, jmh530 wrote:
> On Wednesday, 13 December 2017 at 16:00:32 UTC, Steven Schveighoffer wrote:
>> A nice puzzle for those template gurus out there.
>>
>> I have a function like this:
>>
>> auto foo(T: V[K], V, K)(T t)
>> {
>>    RealAAType!(T, V, K) aa = t;
>>    return aa;
>> }
>>
>> So I need to know what to write RealAAType. What I'm looking for is a mechanism to write the exact AA type that is passed in. RealAAType should be an AA, and nothing else.
>>
> [snip]
> 
> How about:
> 
> template RealAAType!(T, V, K)
> {
>      import std.traits : CopyConstness;
> 
>      alias RealAAType = CopyConstness!(T, V[K]);
> }

Yes, that seems to work, I was worried that something like this would give me multiple type modifiers (.e.g const(const(int)[int])

Now, I need to figure out how to do this in object.d :)

BTW, the PR that spurred this for reference: https://github.com/dlang/druntime/pull/1992

-Steve
December 14, 2017
On 12/13/17 3:23 PM, Steven Schveighoffer wrote:
> On 12/13/17 11:30 AM, jmh530 wrote:
>> How about:
>>
>> template RealAAType!(T, V, K)
>> {
>>      import std.traits : CopyConstness;
>>
>>      alias RealAAType = CopyConstness!(T, V[K]);
>> }
> 
> Yes, that seems to work, I was worried that something like this would give me multiple type modifiers (.e.g const(const(int)[int])
> 
> Now, I need to figure out how to do this in object.d :)
> 
> BTW, the PR that spurred this for reference: https://github.com/dlang/druntime/pull/1992

After thinking about it, I've decided to simply use the workaround I implemented, just apply const to the AA in all cases, since I have to cast to void * anyway.

Thanks for the tips, I learned a little bit here :)

-Steve