On Nov 30, 2013, at 1:07 AM, David Rajchenbach-Teller <dtel...@mozilla.com> 
wrote:

> On 11/30/13 10:01 AM, Kevin Ballard wrote:
>> `new` isn't self-documenting. It's merely consistent with C++, but
>> consistency with C++ cannot be considered self-documentation because
>> there are a great many programmers out there who have never touched C++
>> (or any language with a `new` operator). To someone who hasn't
>> encountered `new` before, an equally-valid interpretation of `new Foo`
>> would be to construct a `Foo` value by invoking a standard initializer,
>> returning it by-value.
> 
> I believe that Patrick's argument is that the primary target of Rust is
> people who are already familiar with C++. To them, `new` will be
> self-documenting. Programmers who are not familiar with `new` (who are
> getting rare these days) will still need to learn a new construct, just
> as they would with sigils.
> 
> I do not have a very strong opinion, but I believe that `new` is indeed
> a good way to astonish the least amount of newcomers.

Assuming for the moment that most Rust programmers will, both now and in the 
future, have started with C++, we still need to document the allocation 
operator in such a way that people who have not seen it before will understand 
it. Given that, I do not see that there is much benefit to using `new` over ~ 
for the sake of familiarity to a subset of programmers, as that does not 
absolve us from our need for good documentation. Not to mention the proposed 
`new` operator here will actually behave differently than it does in C++ 
anyway, which means that familiarity might actually be a bad thing as it will 
lead users to think they understand the operator when they don't! Notably, in 
C++ the `new` operator invokes a class constructor, whereas in Rust it 
allocates an arbitrary expression's value into a memory location (typically on 
the exchange heap, or into an explicit location with placement new). What is a 
C++ programmer supposed to think when they see `new (x + y)`?

Speaking of `new (x + y)`, has any thought been given to differentiating `new 
(expr)` from placement-new? AFAIK whitespace after keywords/identifiers is not 
significant anywhere else in the language, so one would expect `new (foo)` to 
be an attempt at placement-new with the destination expression `foo`, just like 
`new(foo)`. But if that's the case, how does one allocate the value of a 
parenthesized expression? `~(x + y)` is perfectly legal today. The only 
appropriate solution I can think of is `new() (x + y)`, which is ugly and 
surprising.

Given all this, my inclination at this point is to say that trying to piggyback 
on C++ programmers' familiarity of `new` is actually a really bad idea, as 
Rust's allocation operator differs in a few important ways from C++. In fact, 
offhand I can't think of any language with a `new `operator that uses it for 
something other than invoking a class constructor.

-Kevin
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to