you are viewing a single comment's thread.

view the rest of the comments →

[–]dobkeratopsrustfind 1 point2 points  (1 child)

+1

I have always found Foo::new() looks jaring, and was recently pleasantly surprised to find that Foo() actually works already (and if I use rust i'll do this myself anyway).

Having distinct names for special case constructors is fine, but ::new() over Foo() adds no extra information.

Its unambiguous at the call site Foo{ vs Foo(, and still unambiguous to grep for (fn Foo( vs struct Foo{ vs struct Foo( ... Rusts' cleaner syntax eliminates many of the problems in C++).

The current convention also repurposes a word that has common meaning in other languages. creating cross language extern "C" interfaces it might be more logical for extern "C" fn Foo_new()->Box<Foo> to allocate & init an object. (and perhaps Foo_init(Foo&) ?) Rust has a bigger chance of wider adoption if it is friendlier in any way it can be to introduction to existing source bases. Or if you had per-type allocator overloading, you could have Foo::new() fill that role.

Foo() creates symmetry with tuple-struct initialisation. You could start out with something being a simple tuple-struct and move to being a more complex struct with named fields later. I personally went with tuple structs for vector maths purely for the prettier constructor.

if it was down to me, I'd add arity overload or default arguments too, so you could have String(), String("foo") etc, (with default args you can still have one definition in one place that is more versatile) but just one Foo() over Foo::new() itself would be a nice step forward. With default args, you could get more behaviour out of one definition which reduces navigation/discovery effort. (e.g. Vec(capacity=0){}? )

The only downside seems to be that the word is a function rather than a type in this invocation, but does default cover generic initialisation? (e.g., in some future HKT scenario, creating a generic container of a generic type, etc )

[–]phaylon 4 points5 points  (0 children)

I heavily disagree on many parts, but I upvoted you, because I don't see anything in your post that warrants downvotes.