[rust-dev] proposal: unify module/type namespaces

Niko Matsakis niko at alum.mit.edu
Thu Aug 16 09:08:27 PDT 2012


Currently modules and types occupy distinct namespaces. I propose that 
we merge them into one. The main reason to do this is that we can write 
something like:

import foo::bar::MyType;
...
let instance = MyType::new()

where `new()` is a static fn (hereafter, selfless fn, because it is 
technically accurate but also mildly amusing). This resolves the 
constructor naming issue. It also changes how selfless fn names work: 
rather than floating out to the containing module, they would be 
contained within their type (which seems more natural). Coherence makes 
this well-defined; inherent selfless fns are associated with their 
inherent type, and selfless fns that fulfill a trait can be invoked via 
the trait version.

Currently have a number of modules named after the primitive types, 
e.g., uint::max(). These can be rewritten as static functions. In fact, 
they probably *should* be static functions on some kind of `Number` or 
`Integer` trait anyhow. As a side benefit, we can make `uint`, `int`, 
and other primitive type names true keywords.

It should be possible to import selfless fns as normal. With some more 
effort, we could also allow importing non-selfless fns—this would yield 
a function where the receiver is the first argument. This should help 
satisfy the desire expressed by various Haskell programmers to avoid dot 
notation. It would also sometimes be useful for higher-order functions.

For example, `max()` can be defined as a method like so:

trait Number {
fn max(self, other: self) -> self;
}

and then used as follows:

num_a.max(num_b)

or as follows:

import Number::max;
...
max(num_a, num_b)

whichever is more convenient or more aesthetically pleasing.


Niko


More information about the Rust-dev mailing list