Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > polymorphism in C

Reply
Thread Tools

polymorphism in C

 
 
Edward A. Falk
Guest
Posts: n/a
 
      12-18-2012
In article <LFGzs.702744$(E-Mail Removed)4>, BartC <(E-Mail Removed)> wrote:
>
>(I've used polymorphism in my language projects for many years (I've only
>recently discovered there was a special word for it!) The latest one is
>implemented (indirectly) in C.


I would be interested in seeing a sample of how you did it.

--
-Ed Falk, http://www.velocityreviews.com/forums/(E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      12-18-2012


"Edward A. Falk" <(E-Mail Removed)> wrote in message
news:kaqegf$7si$(E-Mail Removed)...
> In article <LFGzs.702744$(E-Mail Removed)4>, BartC <(E-Mail Removed)>
> wrote:
>>
>>(I've used polymorphism in my language projects for many years (I've only
>>recently discovered there was a special word for it!) The latest one is
>>implemented (indirectly) in C.

>
> I would be interested in seeing a sample of how you did it.


By 'indirectly' I mean that an interpreter is written in C for the language
in question.

(The data structures I use *could* be used directly in C, and most things
done with functions calls, however my experiments seemed to show that this
wouldn't be much faster than just interpreting bytecode instead).

I also use dynamic typing, which as well as polymorphism (I couldn't tell
you which kind, apparently there are several) gives you generics for free!
But dynamic typing is also much slower (to execute) than a C++-like
approach.

The technique uses tagged data, which can be implemented in many ways. This
is how it might be done in Lua:

www.lua.org/doc/jucs05.pdf

(on page 5, using a couple of tagged unions). However my language is richer
and more ambitious, and so the unions are a bit more complex (see below).

In brief, every data type is stored inside a 16-byte struct, of which the
first 2 bytes are the type-tag. The data value has to fit inside the rest
(simple values are stored directly, others via a pointer to heap data).

This one struct is passed and returned by value everywhere (if GC was used,
perhaps passing a pointer to the struct would do; but I don't have GC).

Most of the actual type-dispatch (either single or double) is taken care of
inside the interpreter (in the C version, using switch statements;
previously, using lots of jump and call tables). It is unusual for an
application in this language to need or want to know the actual type of
anything. It's just not necessary!

(My data union. This uses anonymous unions and structs that might be
non-standard. i32 etc. are typedefs:

typedef struct _varrec {
union {
struct {
u16 tag;
u8 copy;
u8 alloc;
};
u32 tagx;
};

union { /* .value3 */
int value3;
int lower;
struct {
u16 elemtype;
union {
i16 shortlower;
struct {
i8 bitslower;
i8 bitoffset;
};
};
};
int cdata;
};

union {
struct {
union { /* .value */
int value;
uchar* svalue;
byte* ptr;
int* iptr;
struct _varrec* vptr;
int* retaddr;
};

union { /* .value2 */
int value2;
int length;
int upper;
};
};
i64 dvalue;
r64 xvalue;
};

} varrec;

)
--
bartc

 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      12-18-2012
On 12/17/2012 07:20, Shao Miller wrote:
> On 12/17/2012 06:40, BartC wrote:
>>
>> (And that sample class seems inefficient to me: having to store
>> details of
>> all four functions - and perhaps a lot more - in every single instance of
>> the class. Which also need initialising for each instance. I'm sure that
>> information - the list of functions - only needs to exist in one place.
>> Although as written, it makes the call syntax simpler in C.)
>>

>
> I would not expect that the constructs that were shared would be used in
> "an object having a certain class," but only in the instances of a class
> itself.
>
> That is, 'myclass' seems indicative of "a new class" rather than "a new
> myclass." I'd guess that "myobj" might have its first member as a
> 'Class *', thus saving most of the storage requirement you mention.
>
> Obviously I could be wrong, as I've no idea which book this is inspired
> by. I'd read a PDF (for this sort of thing in C) once where "class
> objects" and "class instance objects" were distinct subjects, though.


Aha. I found the PDF I had read before. It seems convincingly to be
the OP's book (see the bottom of page with label "12"):

http://www.cs.rit.edu/~ats/books/ooc.pdf

- Shao Miller
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-18-2012
"Shao Miller" <(E-Mail Removed)> wrote in message
news:kaqp44$enm$(E-Mail Removed)...
> On 12/17/2012 07:20, Shao Miller wrote:
>> On 12/17/2012 06:40, BartC wrote:
>>>
>>> (And that sample class seems inefficient to me: having to store
>>> details of
>>> all four functions - and perhaps a lot more - in every single instance
>>> of
>>> the class. Which also need initialising for each instance. I'm sure that
>>> information - the list of functions - only needs to exist in one place.
>>> Although as written, it makes the call syntax simpler in C.)
>>>

>>
>> I would not expect that the constructs that were shared would be used in
>> "an object having a certain class," but only in the instances of a class
>> itself.
>>
>> That is, 'myclass' seems indicative of "a new class" rather than "a new
>> myclass." I'd guess that "myobj" might have its first member as a
>> 'Class *', thus saving most of the storage requirement you mention.
>>
>> Obviously I could be wrong, as I've no idea which book this is inspired
>> by. I'd read a PDF (for this sort of thing in C) once where "class
>> objects" and "class instance objects" were distinct subjects, though.

>
> Aha. I found the PDF I had read before. It seems convincingly to be the
> OP's book (see the bottom of page with label "12"):
>
> http://www.cs.rit.edu/~ats/books/ooc.pdf


Yes, I see what you mean now. So 'Class' is actually the name of a
type-description struct, rather than an ill-advised name for a user object
type. In the actual example, 'String' is the name of the user type, and the
first element points to that table, the way I remember seeing this stuff
done before.

So the actual access from C *will* be a little messier, but that code only
needs to be present in a few places.

(And I haven't read the whole thing, but I would just have used an array
instead of the Class struct, using enumerations for the different kinds of
handlers. Actually I've just seen class hierarchy, metaclassing and
inheritance mentioned - this is where I bail out I think...)

--
Bartc

 
Reply With Quote
 
Edward A. Falk
Guest
Posts: n/a
 
      12-19-2012
In article <7x5As.1278006$(E-Mail Removed)4>, BartC <(E-Mail Removed)> wrote:
>
>
>"Edward A. Falk" <(E-Mail Removed)> wrote in message
>news:kaqegf$7si$(E-Mail Removed)...
>> In article <LFGzs.702744$(E-Mail Removed)4>, BartC <(E-Mail Removed)>
>> wrote:
>>>
>>>(I've used polymorphism in my language projects for many years (I've only
>>>recently discovered there was a special word for it!) The latest one is
>>>implemented (indirectly) in C.

>>
>> I would be interested in seeing a sample of how you did it.

>
>By 'indirectly' I mean that an interpreter is written in C for the language
>in question.


Ahhh, got it. I've done the same thing; just didn't know it had a name.

>The technique uses tagged data, which can be implemented in many ways. This
>is how it might be done in Lua:
>
>www.lua.org/doc/jucs05.pdf


Thanks; I'll give it a read.

--
-Ed Falk, (E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
JimB
Guest
Posts: n/a
 
      12-23-2012
James Kuyper wrote:
> On 12/17/2012 02:27 AM, Keith Thompson wrote:
> ...
>> In C++, non-static member functions take an implicit first argument
>> that's a pointer to the current object; the pointer can be referred
>> to inside the function as `this`. C has no such mechanism, which
>> is why it all has to be done explicitly. (And it can be argued
>> that if you're doing this kind of thing, you should probably use
>> a language that has better support for it, whether that's C++
>> or some other OO language.)

>
> I'm sure you know this, and it's implicit in what you've said, but for
> Jack's sake


Did you mean "Jake"? Well yeah, he is noticeably absent, and also are the
meeting results of the C committee. The nothingness of C looking for a
scapegoat? (Moi?). Yes, C is dead. DEAD!

I of course am an asshole for knowing why C sucks? Same old, aint it? C
programmers went to the moon on masturbation, well then surely all can be
had upon masturbation. So is C.

You are "programmers", right? All I say is in confidence right? Well shut up
and "hear me out (?) (I'm not you):

You leave high school pimply and inadequate ("the girl you love", did the
girl they took from you set your stage? Isn't that just right: you give
opportunity to the evil law (oh, did I feign missing the quotes:
psychologists working for the judges working with "the states" attorneys...
I know "states attorney", very well. But would I dare? Would someone in a
nazi death camp defy the automation-killing-of-people gestapo?).

But I digress. Or I get drunk? It gets worse, huh. I get drunk and then get
mad and "tell you what I really think". Huh. And, "You can't handle the
truth." Because you in your girly-whites don't know... yes you do, hmm?
ASSHOLE: your JOB is to .. , bitch, what is your job? Pftt, you have none,
you are rapist. Rape is rape. Answer: NO. War: YOU die, for YOUR war.
Hello? YOU are fired.

Me done? Are you ****ing kidding? You can't pay me. You are over your limit.
Go vote, cuz at least now it is verbal: rape is rape. So, go **** yourself
"Mr. President".

****

James K: wasn't talkin to ya or at ya or back at ya.. just having a "good
time" gettin drunk in detroit!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
Polymorphism in GridView (ASP.Net 2.0) Kevin Lawrence ASP .Net 0 02-16-2006 11:32 AM
polymorphism Tony Morris Java 1 03-07-2004 01:29 AM
Help : Casting after a clone & method polymorphism Osaman Java 2 01-05-2004 03:59 PM
Polymorphism in xml ?? Sony Antony Java 0 08-26-2003 03:37 PM



Advertisments