Skip to content

Instantly share code, notes, and snippets.

@thewilsonator
Created October 9, 2016 06:00
Show Gist options
  • Save thewilsonator/06fe58b06eb7b2d97a675f95bb4d3fac to your computer and use it in GitHub Desktop.
Save thewilsonator/06fe58b06eb7b2d97a675f95bb4d3fac to your computer and use it in GitHub Desktop.
import std.array;
import std.algorithm;
import std.traits;
import std.string;
class Test
{
inout(int) f() inout { return 10; }
void t()
{
static if (is(this == const))
{
auto d = &this.f;
enum s = "alias p = "~typeof(d).stringof~";";
mixin(s.replace("inout","const"));
// pragma(msg ,s.replace("inout","const"));
d();
}
else static if (is(this == immutable))
{
auto d = &this.f;
enum s = "alias p = "~typeof(d).stringof~";";
mixin(s.replace("inout","immutable"));
//pragma(msg ,s.replace("inout","immutable"));
d();
}
else
{
/* f(); // calls fine with mutable 'this'
auto d = &this.f;
pragma(msg, typeof(d).stringof);
enum s = "alias p = "~typeof(d).stringof~";";
enum ss = s.replace("inout","");
enum sss= s[s.minPos("(").front];
pragma(msg ,s.replace("inout",""));
//mixin(s.replace("inout",""));
d();*/
}
}
}
void main()
{
//const ct = new Test();
//ct.t();
auto at = new Test();
at.t();
//immutable it = new Test();
//it.t();
}
struct S
{
inout(int)[] f(ref const(int) arg) inout pure nothrow { return [ arg ]; }
}
string bashing(T, dg, string method)()
{
string ss = "alias delegateTypeForInoutMethod = " ~dg.stringof.replace("function","delegate")~ ";";
static if(is(T== const U,U))
{
ss = ss.replace("inout","const");
ss = ss[0.. ss.lastIndexOf("const")] ~ ss[ss.lastIndexOf("const") + "const".length .. $];
}
else static if (is(T== immutable U,U))
{
ss = ss.replace("inout","immutable");
ss = ss[0.. ss.lastIndexOf("immutable")] ~ ss[ss.lastIndexOf("immutable") + "immutable".length .. $];
}
else
{
ss.minPos("(").front = ' ';
ss.minPos(")").front = ' ';
}
return ss;
}
template delegateTypeForInoutMethod(T, string method)
{
//pragma(msg,"alias dgtype1 = typeof(&(Unqual!T)" ~ method ~ ");");
enum s = "alias dgtype1 = typeof(&Unqual!T." ~ method ~ ");";
mixin(s);
pragma(msg,bashing!(T,dgtype1,method)());
mixin(bashing!(T,dgtype1,method));
}
alias b = delegateTypeForInoutMethod!(const(S),"f");
static assert(is(delegateTypeForInoutMethod!(const(S), "f") == c), "You failed!");
alias c = const(int)[] delegate(ref const(int)) pure nothrow;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment