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{ ... }

No other functions would follow such conventions. This is also harder to write reliable lint checks for. I'm disinclined to do this.

Naming them after their type, but lowercase, is crummy in other ways:

    type DVec = ...;

    fn dvec() -> DVec { ... }

Now to use DVec you're going to import two symbols: `import dvec::{DVec, dvec}`. All those dvecs ... so unsightly. This is mostly the scheme we will be left with after the conversion to camel case types, but I hope we can come up with something better before 0.4.

One option:

Constructors are called `new`, which will be freed up from the keyword list with the removal of classes. If there are multiple constructors in a module then they are descriptively disambiguated, as in `new_petunia` or `new_petunia_with_vase`. Most modules define one public type they are concerned with, so a simple `new` should suffice.

The general pattern for defining a new 'object' would be:

    mod belt_buckle {

        struct BeltBuckle { ... }

        impl BeltBuckle {
        }

        fn new() -> BeltBuckle { ... }

        fn new_with_gold_plating(plating: GoldPlating) -> BeltBuckle {
            ...
        }
    }

Then to use it:

    import my_crate::belt_buckle;

    let my_belt_buckle = belt_buckle::new();

You will mostly not import the constructor because it will need to be disambiguated from all the other 'new's in the world.

Regards,
Brian

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to