Asking types about their traits (D 1.0)
Burton Radons
burton-radons at shaw.ca
Sun Feb 10 23:21:41 PST 2008
Jarrett Billingsley Wrote:
> "Burton Radons" <burton-radons at shaw.ca> wrote in message
> news:fooeas$1lkd$1 at digitalmars.com...
> >
> > That seems the incorrect type though; it should be "T function (T, inout
> > T)", which works correctly. The only problem with that is that variadic
> > functions would be "T function (..., inout T)", which actually makes sense
> > to the ABI (that implementations must conform to), so why not allow it, at
> > least for types? It would definitely be an easier solution than any
> > C++-style member-function nonsense.
>
> I guess what you're getting at is that you would expect the parameter list
> to include the "this" pointer?
>
> The thing is that a delegate does not use the same calling convention as a
> normal function, so there's no way to implicitly convert between the two
> without thunking. It's why the 'this' pointer is not included in the
> parameter list of the address of a member function. That function pointer
> is just half of what you need to call it, the other half of course being the
> object to call it on.
Dude, no thunk needed. What I said works - EAX is used for the last argument, and it's used for the "this" argument when calling functions, so reconfiguring as I said works for structs and it works for classes:
import std.stdio;
struct Struct
{
int field;
void func (int value)
{
writef ("Hello from Struct with %s, my field is %s!\n", value, field);
}
}
class Class
{
int field;
void func (int value)
{
writef ("Hello from Class with %s, my field is %s!\n", value, field);
}
}
void main ()
{
Struct s;
Class c = new Class;
s.field = 42;
c.field = 67;
// Don't call this, it crashes the runtime even though it appears to be valid D code!
// (&Struct.func) (16);
// So does this! Why are you compiling invalid code, DMD?
// (&Class.func) (12);
// But this is super!
auto fs = cast (void function (int, inout Struct)) &Struct.func;
fs (16, s); // Prints "Hello from Struct with 16, my field is 42!"
// So is this!
auto cs = cast (void function (int, Class)) &Class.func;
cs (12, c); // Prints "Hello from Class with 12, my field is 67!"
}
Although I'll say that making EAX the LAST argument is bizarre.
> Another point to consider is that GDC doesn't use the same calling
> convention as DMD, so where the 'this' pointer is passed might be / probably
> is different.
Ah! I read "A D implementation that conforms to the D ABI (Application Binary Interface) will be able to generate libraries, DLL's, etc., that can interoperate with D binaries built by other implementations." as "A D implementation conforms to the D ABI". Tricksy standard!
I haven't been in GCC's bowels for five years, but as I remember it frontends have control over calling conventions.
Anyway, regardless of anything, DMD should never compile invalid code like that.
More information about the Digitalmars-d-learn
mailing list