[Feature Request] Rename __init__
to __new__
, and make it a true static method
#3916
Open
1 task done
Labels
Review Mojo's priorities
What is your request?
__init__
to__new__
.@staticmethod
, for consistency with other static methods.fn __new__(out result)
.Long term, we should consider making the syntax for static methods more concise.
What is your motivation for this change?
In the recent nightly releases of Mojo, the
out
convention has been unified with ordinary result slots. This means the following method signatures are equivalent from the caller's perspective:In other words,
__init__
is now officially a static method, that returns a newly constructed instance.This raises some concerns:
__init__
is an instance method that mutates an already existing instance, and returnsNone
. We don't want people to be confused by this discrepancy.__init__
methods are static, they don't need to be decorated with@staticmethod
. This is inconsistent with the rest of Mojo, and will probably confuse people.self
for the first argument of__init__
, which misleadingly suggests that__init__
is an instance method.To address the first concern, @lattner proposes we rename
__init__
to__new__
. This seems like an improvement: In Python,__new__
is the lifecycle method that actually returns a new instance of the class. More precisely, it is a static method whose return type isSelf
. This matches the design of Mojo constructors.To address the second concern, we should require constructors to be annotated as
@staticmethod
.self
argument as the syntax for a static method, as was discussed on Discord. This would give us the best of both worlds.To address the third concern, we should consider settling on a new naming convention for the result slot of a constructor, for example:
fn __new__(out result)
fn __new__(out target)
fn __new__(out dest)
I'd love to hear other suggestions.
The text was updated successfully, but these errors were encountered: