www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Fun with inout

reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
I'm having a lot of problems with meta when inout gets involved.
There's no std.traits that deal with inout, and I think that's a bit of a
hole...

So my situation is, I have X captured by ReturnType!(T.f), and I also have
T.
T.f is "inout(RT)[] f() inout { return ...; }"

I run into the situation where X == inout(RT)[]. I can't declare variables
of that type, and it all goes wrong.
What I need to do, is transform X into RT[], const(RT)[] or immutable(RT)[]
accordingly to T.

First problem, I can't work out how to detect if X is inout, I can't seem
to craft an is() expression that works, and there's nothing in std.traits.
After that, I need to perform the substitution, and that's proving to be
really tricky...

I think a std template could be created which will transform some type
containing inout with the appropriate mutability level taken from some
other type...

Can anyone suggest how I might write that template? I can't work it out >_<
Sep 06 2014
parent reply "Kagamin" <spam here.lot> writes:
template ReturnType1(alias F, TArg)
{
	alias ReturnType1=typeof((){
		ReturnType!F function(inout Unqual!TArg) test;
		TArg testv;
		return test(testv);
	}());
}
Sep 06 2014
parent reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
Interesting. You're attacking it from the ReturnType! angle... I didn't
think of that.

I had to stare at this for like 10 minutes to work out how it works ;)
I think I can use this, but I'm not sure it addresses the hole in phobos?


On 6 September 2014 21:00, Kagamin via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 template ReturnType1(alias F, TArg)
 {
         alias ReturnType1=typeof((){
                 ReturnType!F function(inout Unqual!TArg) test;
                 TArg testv;
                 return test(testv);
         }());
 }
Sep 06 2014
next sibling parent "Kagamin" <spam here.lot> writes:
You can write it inline, it's about 2 lines

ReturnType!(T.f) function(inout Unqual!T) test;
typeof(test(t))... // if you have t of type T
Sep 06 2014
prev sibling parent reply "Kagamin" <spam here.lot> writes:
On Saturday, 6 September 2014 at 11:46:17 UTC, Manu via 
Digitalmars-d wrote:
 Interesting. You're attacking it from the ReturnType! angle... 
 I didn't
 think of that.
Function may not have `this` parameter, then you will need to specify argument type anyway.
Sep 06 2014
parent reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
It'd be preferable to receive a type, rather than a function alias.


On 6 September 2014 22:21, Kagamin via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 On Saturday, 6 September 2014 at 11:46:17 UTC, Manu via Digitalmars-d
 wrote:

 Interesting. You're attacking it from the ReturnType! angle... I didn't
 think of that.
Function may not have `this` parameter, then you will need to specify argument type anyway.
Sep 06 2014
parent reply "Kagamin" <spam here.lot> writes:
Hmm... extract type from function with desired constancy and pass 
it where you expect a ready to use type.
Sep 06 2014
parent reply Manu via Digitalmars-d <digitalmars-d puremagic.com> writes:
Existing code is using ReturnType though, so it'd be handy to be able to
perform this same resolution on a type that I already have...


On 7 September 2014 02:25, Kagamin via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 Hmm... extract type from function with desired constancy and pass it where
 you expect a ready to use type.
Sep 06 2014
parent "Kagamin" <spam here.lot> writes:
Ah, you said X?

X function(inout Unqual!T) test;
typeof(test(T.init))...
Sep 08 2014