August 11, 2013
On Sunday, 11 August 2013 at 19:08:58 UTC, Simen Kjaeraas wrote:
> On 2013-08-11, 20:33, JS wrote:
>
>> I think you're missing the point to some degree(I realize there is a diff between an object and a type, but I should be able to easily get the class size of an object at run time regardless if the object is typed as a base class). The code below does this, but at a cost of verbosity.
>>
>> Here is code that exactly demonstrates what I am essentially trying to do. The only drawback now is having to mixin the template for each class which I would want to automate and it would be nice to wrap the RT and CT methods in subclasses without overhead.
>
> It would appear that some information is lost when calling typeid from an
> interface. I would have expected this to work:
>
> interface I {
>     final
>     size_t size() {
>         return typeid(this).init.length;
>     }
> }
>
> class A {}
>
> class B : A, I {
>     int a;
> }
>
> void test1() {
>     I i = new B();
>     assert(i.size > 0); // Fails.
> }
>
>
> A bit more experimentation shows:
>
> void test2() {
>     B b = new B();
>     I i = b;
>     A a = b;
>
>     assert(typeid(a) == typeid(b)); // Passes.
>     assert(typeid(i) == typeid(b)); // Fails.
>
>     assert(typeid(b).init.length > 0); // Passes.
>     assert(typeid(i).init.length > 0); // Fails.
> }
>
>
> It appears thus that the error is in typeid(interface), which does not
> give the actual typeid.
>
> The workaround is as follows:
>
>
> interface I {
>     final
>     size_t size() {
>         return typeid(cast(Object)this).init.length;
>     }
> }
>
>
> Is this what you want? Is it good enough? I have no idea, as you're
> notoriously bad at describing what you want, but pretty good at
> attacking people.
>
> If you're looking for a no-overhead solution, then this might not be
> good enough. I'm surprised that a virtual function call is fine,
> though.

I attack people because they are arrogant bastards. They do nothing to help but only contribute to the chaos. They deserve to be attacked because arrogant people are evil.

In any case I think your solution might be correct. Originally I was using interfaces and do recall it working with classes but not interfaces but didn't know how to get it to work.

August 11, 2013
BTW, I hope that if you want to be added to the ignore list on my side, you use the script and do the same. I know some will want everyone to see their irrelevant posts but I won't see it and so you will never get a response from me and it just clutters up the NG and distracts from D.


August 11, 2013
On Sunday, 11 August 2013 at 19:06:07 UTC, Adam D. Ruppe wrote:
> On Sunday, 11 August 2013 at 18:33:19 UTC, JS wrote:
>> The code below does this, but at a cost of verbosity.
>
> I don't see what the difference is in functionality - it looks to me that you just reimplemented what the compiler does automatically with typeid.
>
> The way typeid(obj) works is similar to a virtual function call internally, indeed, Class.vtbl[0] is a pointer to its TypeInfo instance.
>
> But anyway, then the typesameinfo contains functions for the class name and size, pretty similar to what you just did.
>
>
> What does your implementation give you that typeid() doesn't?

Well, I am wrapping the stuff. But when I've tried to do essentially l the same thing before using the compiler directly, it simply did not work.

If you read Simen Kjaeraas's post you will see it doesn't work directly with interfaces, which is what I was doing.

Hence, either a bug or anti-feature or whatever, the issue was with D and not some weird thing I was trying to do... All this nonsense is trying to work around D doing stupid stuff.

All I know is that in my code, when trying to get the actual object size, even if derived(which may change the size), I would always get 4. I was using an interface as my base type.

Regardless, there are now two solutions and I have to go back to my production code and figure out which one works best for my situation.

August 11, 2013
On Sunday, 11 August 2013 at 19:33:43 UTC, JS wrote:
> If you read Simen Kjaeraas's post you will see it doesn't work directly with interfaces, which is what I was doing.

Oh, I see it now. I think typeid(interface) gives a different set of info.

This seems to work though:

        writeln(typeid(cast(Object)(f)).init.length);

Just casting the interface to Object before fetching the info.
August 11, 2013
On Sunday, 11 August 2013 at 19:58:26 UTC, Adam D. Ruppe wrote:
> Oh, I see it now. I think typeid(interface) gives a different set of info.

Thinking about it a bit more, this makes sense because an interface is not necessarily an Object - it might also be a C++ class, or a COM object, or maybe even a slice into another vtbl if an Object implements multiple interfaces (not completely sure about the latter, I'd have to read some source, but it seems logical to me).
August 11, 2013
On Sunday, 11 August 2013 at 20:03:27 UTC, Adam D. Ruppe wrote:
> On Sunday, 11 August 2013 at 19:58:26 UTC, Adam D. Ruppe wrote:
>> Oh, I see it now. I think typeid(interface) gives a different set of info.
>
> Thinking about it a bit more, this makes sense because an interface is not necessarily an Object - it might also be a C++ class, or a COM object, or maybe even a slice into another vtbl if an Object implements multiple interfaces (not completely sure about the latter, I'd have to read some source, but it seems logical to me).

Possibly. Since I program to interfaces, every reference I use is referenced to an interface. Needing the correct object size for implementing an allocator was not working as "expected"(at least as one would expect with classes). I could not see anything in the docs that would suggest otherwise(else the light bulb might have went off).

i.e., one would expect, IMO, that tsize would work on interfaces because interfaces do not have a "size" so it should return the class size of the object. (so things work uniformly and we can program to interfaces without having to create workarounds).



August 12, 2013
On Sunday, 11 August 2013 at 18:29:15 UTC, JS wrote:
> On Sunday, 11 August 2013 at 18:18:22 UTC, Dicebot wrote:
>> On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:
>>> This is essentially what I'm doing BUT I am trying to avoid having to repeat the exact same crap in every class because it is time consuming, verbose, and error prone.
>>
>> I was answering to Maxim, I don't give a fuck about your problems.
>
> THEN WHY THE FUCK DO YOU POST ON MY THREAD? You have not helped or tried to help one post. You have only ever tried to shut down any questions or suggestions I have based on your arrogant belief system that everyone and everything revolves around you... yet you continue to invade my posts. Seriously, Why can't you just leave me the fuck along and ignore my posts? Is it difficult? No, it's not. This suggests you intentionally want to make trouble. In any case, I'm through talking to you bastards. I will ignore your post from now on. In fact, I'll write a script at some point to simply remove your posts, Tim's, and others that want to act like little fucking arrogant kids that haven't got their ass kicked hard enough to realize they are not the center of the universe.

I am actually coming round to seeing the funny side :-) It's like a toddler screaming and throwing soft toys at people
August 12, 2013
On 2013-08-11 21:08, Simen Kjaeraas wrote:

> If you're looking for a no-overhead solution, then this might not be
> good enough. I'm surprised that a virtual function call is fine,
> though.

How about this:

interface I
{
    size_t size (this T) ()
    {
        return __traits(classInstanceSize, T);
    }
}

class A : I
{
}

class B : I
{
    int a;
}

void main ()
{
    auto a = new A;
    auto b = new B;

    assert(a.size == 24);
    assert(b.size == 32);
}

-- 
/Jacob Carlborg
August 12, 2013
On Monday, 12 August 2013 at 11:34:25 UTC, Jacob Carlborg wrote:
> On 2013-08-11 21:08, Simen Kjaeraas wrote:
>
>> If you're looking for a no-overhead solution, then this might not be
>> good enough. I'm surprised that a virtual function call is fine,
>> though.
>
> How about this:
>
> interface I
> {
>     size_t size (this T) ()
>     {
>         return __traits(classInstanceSize, T);
>     }
> }
>
> class A : I
> {
> }
>
> class B : I
> {
>     int a;
> }
>
> void main ()
> {
>     auto a = new A;
>     auto b = new B;
>
>     assert(a.size == 24);
>     assert(b.size == 32);
> }

Sorry but this doesn't work. b is a B, not an A.

try

I a = new A;
I b = new B;





August 12, 2013
On 2013-08-12 17:49, JS wrote:

> Sorry but this doesn't work. b is a B, not an A.
>
> try
>
> I a = new A;
> I b = new B;

Right, forgot about the interface.

-- 
/Jacob Carlborg