News for hackers. Stuff that matters.
Re:The Year of C++ Successor Languages by zozbot234 on Monday January 02, 2023 @06:26AM (#34216769)
You don't need exceptions for constructors. You can just use static factory functions with an error return as Rust does, and dispense with constructors altogether.
Re:The Year of C++ Successor Languages by mgaunard on Monday January 02, 2023 @07:18AM (#34217139)
Enjoy your combinatorial explosion of states.

It's not equivalent at all.

Re:The Year of C++ Successor Languages by Yoric on Monday January 02, 2023 @07:41AM (#34217304)
Could you elaborate? Rust manages pretty well without constructors and I'm nearly certain that it doesn't have any kind of combinatorial explosion of states. Same for ML-family languages.
Re:The Year of C++ Successor Languages by mgaunard on Monday January 02, 2023 @08:35AM (#34217659)
See other parts of the thread.
Re:The Year of C++ Successor Languages by orf on Tuesday January 03, 2023 @01:20AM (#34228405)
That’s really not an issue, as pointed out in other parts of the thread.
Re:The Year of C++ Successor Languages by jcelerier on Monday January 02, 2023 @09:38AM (#34218296)
Can you enforce at compile-time that only such a static factory can ever be used for creating the object in Rust? This is the whole point of constructors, they cannot not be used. Otherwise you're just one commit away of creating an object that will not respect the invariants - will you even remember to call this specific factory function in 6 months?
Re:The Year of C++ Successor Languages by jstimpfle on Monday January 02, 2023 @10:15AM (#34218740)
There are enough practices that handle this problem just fine in various ways. Like, make the function that should be used stand out in an obvious way.

If that is the whole point of constructors, it's the best argument why Rust does not have them. Programming is chock-full of "this must be called only by that or in this or that context..." and practically speaking, only a part of them can be handled by language objects and construct/deconstruct semantics.

Plus, C++ gives enough escape hatches to get not-constructed objects or to un-construct objects without them going out of scope. The "guarantees" provided by C++ that require a ton of baggage like ridiculous initializer lists or 17 different kinds of constructors (to the point where it's sometimes almost impossible to tell which will be called), or requiring an out-of-band mechanism (exceptions) to signal construction failure.... are simply not worth it in my book.

Re:The Year of C++ Successor Languages by zozbot234 on Monday January 02, 2023 @10:20AM (#34218800)
You get this by default for any object with non-public fields.
Re:The Year of C++ Successor Languages by ginsider_oaks on Monday January 02, 2023 @10:54AM (#34219234)
Sure, just make the fields of the struct private and you can make a public constructor:

https://doc.rust-lang.org/rust-by-example/mod/struct_visibil...

Re:The Year of C++ Successor Languages by IshKebab on Monday January 02, 2023 @05:34PM (#34224643)
Yeah weird question because you can use the exact same style in C++.