> 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.Very excited at the prospect of this. The current system of defining a normal function and then defining a trait function that simply delegates to the normal function has always felt like needless boilerplate. Having the normal function be autogenerated from the trait function sounds like a dream come true. On Thu, Aug 16, 2012 at 12:08 PM, Niko Matsakis <[email protected]> wrote: > 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 > ______________________________**_________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev> >
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
