Omittable parens is an evil
Koroskin Denis
2korden+dmd at gmail.com
Sat Jul 19 06:32:36 PDT 2008
Sure, it's a handy feature for properties, but it is a source of too many
bugs. I think that it is against the D design of avoiding features that
are error-prone.
Today I once again came across with a problem with them and it's still
open:
class Test
{
void test() {}
}
How do I get a mangle of Test.test?
writefln(Test.test.mangleof); // Should be ok, but prints v, which is
not what I need
writefln((&Test.test).mangleof); // prints PFZv, which is a pointer to
function
writefln(__traits(getMember, new Test(), "test").mangleof); // prints v
(void, return value)
If you have other suggestions, please say, I need the solution. Custom
mangling is not an option.
The following example was shown in bugzilla and it once again proves that
ommitable parens lead to errors:
void main()
{
int run()
{
// do the stuff
return 0;
}
Thread thread = new Thread(run); // run -> &run, this(int) is called
instead of this(int delegate() dg).
}
Why not have special syntax for properties, like:
class Array(T)
{
property T* ptr()
{
return _ptr;
}
property int length()
{
return _length;
}
void resize(int newSize)
{
// code here
}
int capacity()
{
// code here
}
private int _length;
private T* _ptr;
}
Usage:
Array!(int) array = new Array!(int);
int len = array.length;
array.resize = 100; // forbidden
int capacity = array.capacity(); // okay
It is a breaking change, sure, but it solves the design flaw and fix is
trivial.
More information about the Digitalmars-d
mailing list