[rust-dev] Naming conventions for constructors

Brian Anderson banderson at mozilla.com
Thu Aug 16 15:03:15 PDT 2012

On 08/14/2012 04:35 PM, Brian Anderson wrote:
> Hey.
> We need a consistent naming scheme for constructor functions. The
> current convention is to give these functions the same name as the type
> they create, so `core::dvec::dvec` is created by calling
> `core::dvec::dvec()`.
> We are in the process of changing our naming convention for types (as
> well as variants - which will eventually be types) to camel case. So
> very soon now your `Option` type will be created by calling `Some(foo)`,
> because `Some` is both a type and a constructor. Functions, on the other
> hand, will continue to have the lowercase + underscore convention.
> This leaves the question of what to call functions that are
> constructors. We could continue to name them after their types, but it's
> a little weird:
>      fn DVec() -> DVec { ... }
> So now I need an alternate constructor. What do I call it?
>      fn DVecWithParam(arg: foo) -> DVec{ ... }

Niko has convinced me that, under the current namespace rules, sticking 
with the current naming scheme of using the type name is best because 
this is the way that imports work out nicest:

     import belt_buckle::BeltBuckle; // give me the type and constructor

If we do merge the type and module namespaces, then you can still just 
import the type as above and construct it with `BeltBuckle::new()`.

My plan for now is to finish the camel casing of types, then go back 
through and find all the constructors and convert them. Then I will hope 
that we do the namespace merging so we can revisit the constructor issue 

If that's acceptable, then the remaining question is of what to do with 
secondary constructors. I will punt on that for now because it doesn't 
show up much in existing library code.

More information about the Rust-dev mailing list