Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dunder methods? #492

Closed
Moosems opened this issue Dec 31, 2023 · 5 comments
Closed

Dunder methods? #492

Moosems opened this issue Dec 31, 2023 · 5 comments

Comments

@Moosems
Copy link

Moosems commented Dec 31, 2023

Do these exist?

@littlewhitecloud
Copy link
Contributor

littlewhitecloud commented Jan 1, 2024

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

@Akuli
Copy link
Owner

Akuli commented Jan 2, 2024

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
Copy link
Author

Moosems commented Jan 2, 2024

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
Copy link
Owner

Akuli commented Jan 3, 2024

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
Copy link
Author

Moosems commented Jan 4, 2024

I concede all points.

@Moosems Moosems closed this as not planned Won't fix, can't repro, duplicate, stale Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants