www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - OO syntax

reply spir <denis.spir gmail.com> writes:
Hello,

(completely new to D and to the list)

I just read the following in the doc about arrays at http://www.digitalmars=
.com/d/2.0/arrays.html:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
If the first parameter to a function is an array, the function can be calle=
d as if it were a property of the array:

int[] array;
void foo(int[] a, int x);
foo(array, 3);
array.foo(3);	// means the same thing
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Does this OO-like syntax also work with other parameter types? Else, why no=
t? I mean, also, why allow it for arrays?
(Comment: Unifying syntax for type-specific "function-methods" would simpli=
fy and clarify code, don't you think?).

Denis
-- -- -- -- -- -- --
vit esse estrany =E2=98=A3

spir.wikidot.com
Oct 13 2010
parent bearophile <bearophileHUGS lycos.com> writes:
spir:

 Does this OO-like syntax also work with other parameter types?

No, it currently doesn't.
 Else, why not? I mean, also, why allow it for arrays?

It was originally a compiler bug relative to just arrays. Some people have appreciated it, and it became a feature. Some people have asked to extend it to all other types, and maybe this will be implemented, but if you mix it with operator overloading, it gives syntax problems. You have to design such shortcuts with a lot of care, because they are a bit handy, but they may have serious side effects. Often keeping the syntax more clean, with less magic, is the safest and wiser thing to do. Bye, bearophile
Oct 13 2010