
On Nov 19, 2019, at 15:45, Greg Ewing greg.ewing@canterbury.ac.nz wrote:
On 20/11/19 12:14 pm, Andrew Barnert wrote:
Off the top of my head, there’s Smalltalk, which I suspect is where Guido got the idea, and ObjC, which got it from Smalltalk, and Swift, which got it from ObjC,
I don't think Smalltalk/ObjC do quite the same thing. Sometimes you see a pattern like
(SomeClass new) initWithArgs: ...
but that's something you do explicitly -- it's not built into the language.
This is a minor difference. A lot of things in Smalltalk and ObjC are ubiquitous conventions instead of language-supported, and this is one of them. Calling new followed by init* is just a convention, but it’s a convention followed almost ubiquitously.
In Python, the 99% case is automated—you have to opt out of it in your __new__ for the rare exceptions, instead of having to opt in to it everywhere except the rare exceptions. Which I think is an improvement, but it doesn’t really change anything fundamental. It’s still two-phase initialization where new/alloc/__new__ is a class method that can return anything it wants (like a cached object or an instance of a subclass) and init/__init__ is an instance method that finishes setting up whatever it returned.
(In modern ObjC with ARC, it’s not quite “just a convention”, because the compiler assumes that you’re following it and you will break garbage collection, and get a warning but not an error, if you break the rules. But you don’t have to use ARC, and you can go around the conventions rather than breaking them, so I think “just a convention” is close enough.)