modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.11k stars 2.59k forks source link

[Docs] __new__ is not mentioned outside of the Change Log. #3263

Closed nevdelap closed 1 month ago

nevdelap commented 3 months ago

Where is the problem?

https://docs.modular.com/search?q=__new__+

I suspect that maybe it should be mentioned in "Lifecycles and lifetimes" and probably other places.

What can we do better?

I was reading the docs, e.g. https://docs.modular.com/mojo/manual/lifecycle/#lifecycles-and-lifetimes, and wondered about __new__ in Mojo. I'm thinking it should described in that section, and maybe other places.

For example, is it (still) a thing in Mojo? If it is, does it work the same as in Python, if it isn't, how does backward compatibility work? I've used it rarely, but I have used it, so it seemed like an omission especially in the topic of lifecycles and lifetimes.

Anything else?

No response

nevdelap commented 3 months ago

This probably overlaps with #2391 and the inconsistent use of terminology relating to the concepts of contruction and initialization.

arthurevans commented 1 month ago

__new__()was removed from the codebase in March 2023, replaced by __init__(): https://docs.modular.com/mojo/changelog#week-of-2023-03-13

nevdelap commented 1 month ago

How can that be if it is supposed to be going to be a superset of Python? And if it is the case then the documentation should still include reference to __new__ to describe that, no? What do you mean that it is replaced by __init__ - __new__ and __init__ are two different things that serve difference purposes in Python.

Maybe __new__ will reappear in the code base when classes are supported? If so then it would make sense for it to appear in doco at that time.

arthurevans commented 4 weeks ago

Sorry for the confusion. My understanding is that the initializer for a Mojo struct was originally added as __new__(), but the name was soon changed to __init__() because the struct initializer matches the semantics of Python's __init__(), not __new__().

I suspect you're right that the __new__() dunder will reappear when classes are supported. If I understand correctly, the main use case for __new__() is when subclassing an immutable class. Structs don't support subclassing, so this isn't a concern for structs.