Akuli / jou

Yet another programming language
MIT License
11 stars 4 forks source link

Dunder methods? #492

Closed Moosems closed 6 months ago

Moosems commented 6 months ago

Do these exist?

littlewhitecloud commented 6 months ago

It seems that akuli said that jou doesn’t need things like __init__ Through I like it. More information see #249 #421

Akuli commented 6 months ago

Indeed, dunder methods are "magical" by design and I avoid magic in Jou. It is a simple language that basically doesn't do anything that you don't tell it to do. This means it won't call any methods unless you call them, so magic methods don't make sense.

That said, sometimes "practicality beats purity" as Python people like to say. Some simple operator overloading (as in custom_thing + custom_thing) could be very useful for game programming, for example. I personally have done game programming in C (using plain old functions instead of a magic + operator) and I think it works perfectly fine, but to some people, a custom + operation is the only reason why they use C++ instead of C.

Moosems commented 6 months ago

So how do I set custom initialization and attribute code that is uniform over a large codebase? If I have to implement different xyz_to_string() methods everywhere where xyz changes to the class name it causes far too much confusion in my opinion. Surely there must be a way to not have automagic but also uniform sets of methods for classes that one can rely on for important information.

Akuli commented 6 months ago

So how do I set custom initialization and attribute code that is uniform over a large codebase?

In most cases it's easiest to initialize all attributes to zero (or NULL or empty string, depending on the type). A simple instance = TheClass{} does that. If you need only a couple nonzero attributes, you can do instance = TheClass{foo = 1, bar = 2}.

If I have to implement different xyz_to_string() methods everywhere where xyz changes to the class name it causes far too much confusion in my opinion. ... uniform sets of methods for classes that one can rely on for important information.

Jou isn't magical like Python. I will not be adding code that can automagically print/stringify any instance of any class, and recursively do it for all members inside it. I understand why you want that, based on your experience with high-level languages, but this is a lot of magic for a language like C or Jou.

Currently the Jou compiler has a method named print() in many classes. When debugging, I just insert a foo.print() into the middle of the code. To me, this has worked great so far. I know it isn't exactly what you expected, but I'd say it's good enough.

Moosems commented 6 months ago

I concede all points.