[rust-dev] Class UI

Kosta Welke theoneandonlykosta at googlemail.com
Thu Apr 12 00:38:41 PDT 2012

On Apr 10, 2012, at 9:26 PM, Masklinn wrote:

> I was reading
> http://smallcultfollowing.com/babysteps/blog/2012/04/09/rusts-object-system/
> today, and saw the description of the classes definition.
> Next to it, Nicholas notes:
>> I am not fond of the definition of constructors, in particular
> I can only agree, for a simple reason: the example is that of an
> initializer, but uses naming generally used for actual constructors.

Apart from the naming, I always found constructors as used in C, Java, Python, etc. to be quite a bit verbose and tedious. I think Dylan's got this about right: You'd just note whether the value should be initialized with a default value or must appear in the constructor. Consider:

class colored_point {
    let x: int = 0,
        y: int = 0;
    let color: color init-required;

    fn abs() -> int { sqrt(x*x+y*y) }

And using it like this:

let p1 = point(color: red); //0,0
let p2 = point(color: blue, y: 2); //0,2
let p3 = point(color: black, x: 3, y: 5) //3,5
let p4 = point(x: 5) //compile time error: "color" is missing as constructor argument

Of course, you can always supply your own constructor for more complicated stuff. But 95% of the time, this is very concise and helpful.

On the other hand, I'm not sure whether Rust has or wants to have keyword arguments for functions...


More information about the Rust-dev mailing list