Remix.run Logo
▲ marcosdumay 6 hours ago

The thing you are annoyed about in C++ is not constructors. It's the overloading of syntactic operations.

The C++ design bets heavily on that. Just like you can overload an operator, you can also overload the meaning of `=`, `new`, variable declarations, and etc. That necessarily leads to an awful complex language, but also a very powerful one that can potentially lead to very easy to read code in high abstraction levels.

The problem both C++ and Swift have is that you can't really build high abstraction levels, because the languages are designed in a way that makes some fixed concerns creep over your entire code.

▲ninkendo 6 hours ago | parent [-]

I get that for C++ to have all this overloading it needs constructors, but Swift doesn't take the same bet as C++ does when it comes to overloading, yet it still has first-class constructors anyway. So do lots of other languages (Java, C#, TypeScript, etc.) Swift is especially interesting because it's basically a successor language to Objective-C, and Objective-C did not have first class constructors (`-init`/`-initWithFoo:` is just a convention), they chose to add it anyway.

▲marcosdumay 5 hours ago | parent [-]

The convenience vs. designated initialization in Swift is exactly the same kind of attempt to overload normal language syntax.

Outside of that, Swift has only normal OOP constructors, that are something completely different, and almost not a pain at all. Normal constructors are just a control layer on top of memory initialization that lets you write custom rules for it. It becomes necessary when you have protected memory in your objects, because the external code can't just go and set it, but even languages like Python understand it's convenient to have a defined place to set things up.