News for hackers. Stuff that matters.
Re:Can you use a class in C? by diarrhea on Sunday August 13, 2023 @05:18PM (#37114620)
I don’t think that’s a strong argument. It’s just function overloading that some languages enable, others don’t. Not a categorical difference.
Re:Can you use a class in C? by 38 on Sunday August 13, 2023 @06:17PM (#37115130)
OK please show me the equivalent code in C then
Re:Can you use a class in C? by tredre3 on Sunday August 13, 2023 @09:25PM (#37116407)
It's possible with _Generic but there's a lot of caveats. Any seasoned C dev would just have two functions with a prefix or suffix, like cat_greet() and dog_greet().

But if you insist, it would look something like:

    typedef ... cat;
    typedef ... dog;

    void cat_greet(cat *obj) {/*do stuff */}

    void dog_greet(dog *obj) {/*do stuff */}

    #define greet(X) _Generic((X), cat: cat_greet, dog: dog_greet)(X)

    int main(...) 
    {
         cat mycat;
         dog mydog;
         greet(&mycat);
         greet(&mydog);
         return 0;
    }
Re:Can you use a class in C? by 38 on Sunday August 13, 2023 @10:23PM (#37116773)
that is really, really ugly. using methods, I dont have to mess with function prefixes, and I dont have to pollute the global scope.
Re:Can you use a class in C? by ElegantBeef on Monday August 14, 2023 @02:00AM (#37117953)
Again that's this language constraining you to that. With procedure overloading it's not required that you have to have that ugly API. For instance the following is valid Nim:

  type
    Cat = object # these are just 'struct' in a different skin
    Dog = object

  proc greet(c: Cat): string = "Meow"
  proc greet(d: Dog): string = "Bark"

  assert Dog().greet() == "Bark"
  assert Cat().greet() == "Meow"