std.allocator needs your help
Benjamin Thaut
code at benjamin-thaut.de
Mon Sep 23 10:04:43 PDT 2013
Am 23.09.2013 17:02, schrieb Adam D. Ruppe:
> We should really deprecate the new keyword. It'd break like all code
> ever, but with D's templates, there's no need for it, and when it is
> there, it is going to spark problems about replacing global allocators
> or the library allocators being second class citizens.
>
Its not possible to replace new with a library function in all cases.
There are many examples where it breaks (I tried it believe me). Just
let me give a few:
class A
{
class B
{
}
B m_b;
this()
{
// uses the sourrounding context
// not possible with library function
m_b = new B();
}
}
Also there is the problem that D does not have perfect forwarding. That
means if "new" is a library function it will cause additional copies /
moves of structs which are not the case with the buildin new. Next there
are implict conversions of literals. Those work just fine with the
buildin new, but will break with a templated library new.
E.g.
class C
{
this(ubyte a, ubyte b)
{
}
}
new C(5,3); // fine
New!C(5,3); // Error: can not implicitly convert int to ubyte
Unless of course you want to write a template metaprogram that does
those implict conversions. Although the problem would remain that you
can't know if the value you get comes from a literal or from a function
call, variable, etc.
The next thing are arrays. While you get the nice array syntax with the
buildin new, a library new just looks ugly.
new int[5];
vs.
NewArray!int(5); // Can't use New! here because it would conflict with
the New! for creating objects / structs
I'm using library implemented new / delete since over a year and its
annoying and makes the language look ugly and feel strange to use. See
my allocator and New / Delete implementation here:
https://github.com/Ingrater/druntime/blob/master/src/core/allocator.d
I would rather want new to be overloadable and have 2 sets of parameters
new (allocator)(arg1, arg2)
Where "allocator" would go to the overloaded version of new and "arg1"
and "arg2" will be passed to the constructor.
I think its a really bad idea to deprecate new.
Replacing Delete works just fine with a library function though.
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d
mailing list