digitalmars.D - How to extract the AA type?
- Steven Schveighoffer (31/31) Dec 13 2017 A nice puzzle for those template gurus out there.
- jmh530 (9/19) Dec 13 2017 [snip]
- Steven Schveighoffer (7/32) Dec 13 2017 Yes, that seems to work, I was worried that something like this would
- Steven Schveighoffer (6/23) Dec 14 2017 After thinking about it, I've decided to simply use the workaround I
- Jack Applegame (6/6) Dec 13 2017 auto foo(T: V[K], V, K)(T t)
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
Dec 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]);
}
Dec 13 2017
On 12/13/17 11:30 AM, jmh530 wrote:On Wednesday, 13 December 2017 at 16:00:32 UTC, Steven Schveighoffer wrote: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 -SteveA 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]); }
Dec 13 2017
On 12/13/17 3:23 PM, Steven Schveighoffer wrote:On 12/13/17 11:30 AM, jmh530 wrote: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 :) -SteveHow 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
Dec 14 2017
auto foo(T: V[K], V, K)(T t)
{
CopyConstness!(T, V[K]) aa = t;
return aa;
}
https://run.dlang.io/is/LSMa5C
Dec 13 2017









Steven Schveighoffer <schveiguy yahoo.com> 