[rust-dev] Class UI

Masklinn masklinn at masklinn.net
Tue Apr 10 12:26:41 PDT 2012

I was reading
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.

Let's back up to what I mean: the role of a constructor is to construct,
so it would take nothing, allocate a chunk of memory and put object
content in it, then return the type-tagged chunk of memory (or a
reference to it with runtime/GC cooperation).

An initializer is the part the constructor delegates to for the "put
object content in it", all arguments to the constructor are forwarded to
the initializer but the initializer gets a "ready to use" object and
makes it "readier" by initializing fields if needs be.

I realize some languages (mostly in the Java line) call the initializer
"constructor" and give no access to the constructor itself, that's fine.
There's also C++ which gives access to both as respectively the
constructor and *the new operator*.

The latter part hints at what bothers me with the current syntax/naming:
languages giving access to actual constructors (the "class method" which
handles allocation and fully creates an instances from scratch) very
often use the `new` naming: C++ uses `operator new` (called through the
likewise named operator), Python uses `__new__` (called through the call
operator `()`), Ruby uses `new` (called directly on the class), and I
think that's how Perl5 seems to work (the instance is created within
a proc called `new` — from scratch — then blessed and returned).

The only significant differer I've found seems to be F#.

As a result, really don't think Rust should call its initializer `new`.

Although I *do* think it would be neat if Rust provided an actual
constructor which would have to return some sort of instance build
from scratch using core memory allocation thingies.

But whether it provides a constructor or not, it should not name
its initializer `new`.

`init` is sometimes used, at least in javascript "class" libraries
as well as in Objective-C[0] (and in Python, with dunders since it's
a magic method) (Ruby uses `initialize`)

[0] Yeah I know it's a bit weirder in Obj-C as it can return the
    instance as well, but the memory for the instance is setup in
    a preceding `alloc` call so it still works.

More information about the Rust-dev mailing list