Free functions can be used as methods of an array if the first argument in the function definition is an array. For example: int indexOf(T)(T[] array, T value) {...} int[] ints = getInts(); int i = ints.indexOf(2); However, when the array argument is called without parameters, the expression fails to compile, as this example shows. string[] split(string s, string sep) { ... } class Person { string name_; string name() { return name_; } } auto person = getPerson(); string[] firstAndLast = person.name.split(" "); The above line will only compile if it is changed to: string[] firstAndList = person.name().split(" ");
(In reply to comment #0) > Free functions can be used as methods of an array if the first argument in the > function definition is an array. For example: > int indexOf(T)(T[] array, T value) {...} > int[] ints = getInts(); > int i = ints.indexOf(2); > However, when the array argument is called without parameters, the expression > fails to compile, as this example shows. > string[] split(string s, string sep) { ... } > class Person { > string name_; > string name() { return name_; } > } > auto person = getPerson(); > string[] firstAndLast = person.name.split(" "); > The above line will only compile if it is changed to: > string[] firstAndList = person.name().split(" "); Oops, I meant to say: However, when the array argument is called without parentheses, the expression fails to compile...
One should possibly add @property for such function, so it will be possible tu use such function not only as method but also as getter. Unfortunetly it isn't yet implemented.
Similary to the bug3771
Works in D2 (2.058)
*** This issue has been marked as a duplicate of issue 2883 ***