News for hackers. Stuff that matters.
Re:Ideas I would like to build / see built by mkramlich on Saturday April 10, 2010 @10:16PM (#1256183)
Disagree C has classes. No "class" and no inheritance or polymorphism. C++ and Objective-C have these things. But I understand what you're saying. There is the very well-known structs+functions technique which gives a partial stand-in for full OO.

My post was meant to be funny yet true. I read the OA and thought all of the ideas he listed already exist, and was surprised it made the front page on Hacker News. Then I thought to myself, "Oh, it must be because it's a joke, the OA was trying to be funny." Perhaps I was mistaken.

Re:Ideas I would like to build / see built by cperciva on Saturday April 10, 2010 @11:51PM (#1256294)
Disagree C has classes. No "class" and no inheritance or polymorphism

C has perfectly good inheritance:

    struct child {
        struct parent p;
        // new variables go here
    }
    struct child foo;
    struct parent * bar = &foo;  // perfectly valid C
Re:Ideas I would like to build / see built by mkramlich on Sunday April 11, 2010 @12:05AM (#1256321)
Looks like composition and a struct alias, not inheritance to me.

Is my C that rusty?

For example, say that the "struct parent" type (which in your example has no members) had a member named z. In your example, I expect to be allowed to do something like this:

    (*bar).p.z
which is composition. But not:

    (*bar).z
which would be member inheritance. Because bar/foo/child all lack a z member. Only "parent" has a z member. Am I wrong?
Re:Ideas I would like to build / see built by cperciva on Sunday April 11, 2010 @12:19AM (#1256341)
It's valid C to cast a structure to the type of its first element. So in the example of

  struct child {
    struct parent {
      int z;
    } p;
  } foo;
you can access foo.p.z but not foo.z -- but you can access (struct parent *)(&foo)->z, so you can pass foo to anything which expects a struct parent.
Re:Ideas I would like to build / see built by mkramlich on Sunday April 11, 2010 @12:42AM (#1256360)
I'll assume you're right. Thanks for pointing that out. Your C sounds fresher than mine. I used it heavily from 90-95 or so but later moved on to higher languages like Java and Python. If what you point out about the syntax is accurate, I find that to be a counter-intuitive design decision.
Re:Ideas I would like to build / see built by mhansen on Sunday April 11, 2010 @02:08AM (#1256442)
It works because C structs are just contiguous bits of memory, interpreted as various fields. A pointer to a struct points to the start of that contiguous bit of memory.

  Child Struct:
  +---------------+-----------------+
  | parent struct | child fields    |
  +---------------+-----------------+
  ^
  \--- pointers to the struct point at the start
You can cast this child struct to a parent struct, which is like saying 'interpret memory starting here as a 'parent' struct'.

This works: The memory from location to location+sizeof(parent) IS a parent struct. The child fields following the parent struct are simply ignored.

Re:Ideas I would like to build / see built by cperciva on Sunday April 11, 2010 @05:08AM (#1256594)
It works because C structs are just contiguous bits of memory, interpreted as various fields

Well... almost. C structs can have internal padding -- but the C standard doesn't allow padding before the first element, so that first element is guaranteed to begin at the beginning of the struct.

Re:Ideas I would like to build / see built by mhansen on Sunday April 11, 2010 @07:38AM (#1256708)
Thanks! You learn something every day :)
Re:Ideas I would like to build / see built by blasdel on Sunday April 11, 2010 @01:35AM (#1256421)
OOP != Class Inheritance
Re:Ideas I would like to build / see built by ahk on Sunday April 11, 2010 @04:34AM (#1256574)
Ok, so what else is it? My understanding that the new Go language is not officially called object-oriented because it supports only interfaces and not inheritance or type hierarchy.
Re:Ideas I would like to build / see built by blasdel on Sunday April 11, 2010 @06:17AM (#1256643)
Go has true message passing, which is far more important to OOP than anything to do with data modeling, and Go's composition model for data is far more object-oriented than the usual Linnaean classicism. Pay attention to Alan Kay, not Stroustrup or Gosling.

If you really want to know what OOP is / was / can be, read Cardelli and Abadi's A Theory of Objects. The first half is available online, and covers the parts you'd care about: http://books.google.com/books?id=4xT3LgCPP5UC