Skip to content

Comment on Making a StringBuffer in C, and questioning my sanityparent

Comments

????

Unfortunately, you will need to cover more ground with this proposal than what you have presented. It sounds simple but what you are suggesting is very complicated. Of course, to the OOP minds out there, they think their suggestion is a be-all-to-end-all, when in reality there are many ways to solve a problem.

I understand what you are trying to do. I am not suggesting it is wrong but C is not an OOP language. I dont see why we should PRETEND that it has OOP-class features when its all you advocating is smoke and mirrors.

Sure, C can do "OOP" but I must remind all that these techniques have been in C for years, before the OOP name was popularised and evolved into the form it is now. You are just forcing C programmers to program in such a way OOPLOVERS like when it is not required - and that is the modern OOP than has been forced on everything since the early-to-mid 90's.

You are just HIDING whats really going on

This proposal you are suggesting -- I am going to call this Typescript-C.

The problem is that this namespace/class overlay makes much ASSUMPTIONS what the programmer wants. Lets dig into this further. Here is how (I believe) you see it implemented :-

  // In my TypeScript-C.
  namespace Foo {

    class Bar {

      int baz;

      void init() {
        this->baz = 10;
      }

      void free() {
        // whatever
      }

      int process(int withval) {
        return withval + this->baz;
      }
    }
  }

  // calling example :-
  Foo.Bar sample;
  sample->init();
  printf("%d\n", sample->process(100));
  sample->free();
This would translate to :-
  // Translated C...
  typedef struct Foo_Bar {
    int baz;
  }

  void Foo_Bar_init(Foo_Bar *self) {
    self->baz = 10;
  }

  void Foo_Bar_free(Foo_Bar *self) {
    // whatever
  }

  int Foo_bar_process(Foo_Bar \*self, int withval) {
    return withval + self->baz;
  }

  // calling example :-
  Foo_Bar sample;
  Foo_Bar_init(&sample);
  printf("%d\n", Foo_Bar_process(&sample, 100));
  Foo_Bar_free();
My argument is, for starters, how is writing all this namespace and class wrapper actually better than the final code? Anyone that starts using C with namespaces and classes are going to assume we have inheritence, overrides, etc. Why?

If anyone is THAT bad they cannot write proper C code, then I suggest they move to C++, Java, C#, Dlang.. or whatever.

Its just adding extra fluff -- and C guys like to SEE what is going on. (Thats why it takes ages to truly grasp C++ internals)

I'm thinking golang style OOP - basically structs with syntactic sugar of dot-calling. Nothing more. We wouldn't want full blown vtable C++ style inheritance with polymorphism, operator overloading etc. because this would create another C++ (D++ maybe? ;)

However while we're looking at any library, GTK for example, those structs are made like that. But you're right - this only looks simple, as there are many pitfalls we could hit when implementing it.

Personally - I think C just needs to remain as C. Sure there will be improvements along the way... but whenever I use C, I stick with C99.

There are many alternatives or newer languages which I think is the answer to your proposal.

You mentioned Go, which is absolutely reasonable.

However, like all languages, Go has its pros and cons as well. I am sure someone has tried to push for a new feature that may be OOP-like and it gets rejected. You can say that for any language.

We also have Rust, Odin, Zig, C3, etc -- all have their own sprinkles and flavours on how to do things. These are "better C" or "better C++" depending on how you view it.

Personally I prefer Odin. It (kinda) has namespaces but there are no classes... and functions/procedures are still created outside the struct. To me, out of the many languages, Odin keeps it C but with many improvements and I am happy with that.

Each to their own.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.