News for hackers. Stuff that matters.
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++.