Unified function call syntax
Denis Koroskin
2korden at gmail.com
Mon Sep 8 02:34:06 PDT 2008
On Mon, 08 Sep 2008 11:11:30 +0400, Sergey Gromov <snake.scaly at gmail.com>
wrote:
> Manfred_Nowak <svv1999 at hotmail.com> wrote:
>> Sergey Gromov wrote:
>>
>> > you essentially define (semantically)
>> >
>> > class char[] {
>> > bool equals(char[] b) {...}
>> > }
>>
>> But then one semantically inherets that class---and therefore should be
>> able to override/overload the inhereted methods, but you deny this.
>> Why?
>
> You cannot derive from an array. When you define
>
> class A {
> bool equals(Object o) {...}
> }
>
> you define a method, A.equals(), which cannot be called for an object of
> char[] class.
>
> Of course not everything is that nice. Consider
>
> bool equals(char[] a, char[] b) {...}
> class A {
> bool equals(char[] a, char[] b) {...}
> bool foo() {
> char[] x, y;
> bool r1 = equals(x, y); // I expect A.equals() to be called
> bool r2 = x.equals(y); // I expect .equals() to be called
> }
> }
>
> Here syntax dictates different expectations for something which
> semantically must be the same. So I personally put this 'unified
> function call' feature in the same basket with property call syntax: a
> 'sugar' which brings more ambiguity to the language than it actually
> helps rapid development.
There is not that much of an ambiguity. First of all, member functions can
not be used as Unified Function Call, only free ones. That's why your
example is perfectly valid, I think that everyone expect the same
behaviour. It's just a compiler who is doing things wrong (and bugs are
already reported).
Second, I suggest marking functions to be used as a UFC or their first
argument with something. Examples:
bool equal(char[] this, char[] other) // note that you can't normally use
`this' as an argument in functions. I like this one
{
return this[] == other[];
}
or
bool equal(this char[] first, char[] second) // that's the approach used
in C#
{
return first[] == second[];
}
so that other function won't be misused as in the following example:
void copy(void[] dst, void[] src)
in
{
assert(dst.length >= src.length);
}
body
{
memcpy(dst.ptr, src.ptr, src.length);
}
int[] array1, array2;
array1.copy(array2); // what is going to be copied and where?
More information about the Digitalmars-d
mailing list