Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 18, 2005 Undocumented string functionality? | ||||
---|---|---|---|---|
| ||||
I tried to split a few lines and found this handy (?) functionality. It seems that all functions in std.string can be used as properties with char[] - strings. For example std.string.split("something") == "something".std.string.split(). I suppose any other function outside std.string cannot be used like this. I can't find any notes explaining this in the manual. So, are these functions officially some kind of properties for the char[] type? I couldn't find any direct binding code in phobos/std/string.d. (compiler magic, perhaps) If this is normal use of string functions, why do we need to import std.string to use them? I mean if these functions are implicitely char[] properties, why do we need to explicitely import std.string? |
October 18, 2005 Re: Undocumented string functionality? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | Jari-Matti Mäkelä wrote: > I tried to split a few lines and found this handy (?) functionality. It seems that all functions in std.string can be used as properties with char[] - strings. For example std.string.split("something") == Works also for all types of arrays. > "something".std.string.split(). I suppose any other function outside std.string cannot be used like this. Yes it can. > I can't find any notes explaining this in the manual. > It isn't there. > So, are these functions officially some kind of properties for the char[] type? Wouldn't we all like to know :) Walter is the only one that can say is this a feature or a bug. I hope it is a feature that is going to stay. > I couldn't find any direct binding code in phobos/std/string.d. (compiler magic, perhaps) > > If this is normal use of string functions, why do we need to import std.string to use them? I mean if these functions are implicitely char[] properties, why do we need to explicitely import std.string? They are not implicit properties. Try something like: float bla(char[] str, int x) { return str.length * x; } char[] str = "hello"; float f = str.bla(5); |
October 18, 2005 More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | Property function calling syntax works with any function. For instance: writefln = "asdf"; is the same as writefln("asdf"); Unfortunately this cannot be combined with the array function calling to create something like: char[] aeou(char[] a, char[] b); writefln = ("foo".aeou = "bar"); Which is such a shame <g> |
October 18, 2005 Re: More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | IIRC the general case is as this: int fn(T*, ... ){...} T a; a.fn(...); if the first param is a pointer type than the function can be used as a proerty on somthing of that type. I think this is NOT a bug (walter posted about this some time ago) just not documented. (Somthing more for the todo list) |
October 18, 2005 Re: More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | Hmm.... I must say I would see this one (fcall = param) being a source of bugs, but that's just me.
-[Unknown]
> Property function calling syntax works with any function. For instance:
>
> writefln = "asdf";
>
> is the same as
>
> writefln("asdf");
>
> Unfortunately this cannot be combined with the array function calling to create
> something like:
>
> char[] aeou(char[] a, char[] b);
>
> writefln = ("foo".aeou = "bar");
>
> Which is such a shame <g>
|
October 18, 2005 Re: More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | Deewiant wrote:
> Property function calling syntax works with any function. For instance:
>
> writefln = "asdf";
>
> is the same as
>
> writefln("asdf");
>
> Unfortunately this cannot be combined with the array function calling to create
> something like:
>
> char[] aeou(char[] a, char[] b);
>
> writefln = ("foo".aeou = "bar");
>
> Which is such a shame <g>
WOW! I didn't know this works.
int sq(int x){return x*x;}
int y = 5;
y = (sq = y);
writefln(y);
//y is 25;
But this looks like a bug as i don't remember seeing this in the doc.
|
October 18, 2005 Re: More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote:
> WOW! I didn't know this works.
>
> int sq(int x){return x*x;}
> int y = 5;
>
> y = (sq = y);
Now this looks terrible! I'm sure this is not what Walter had originally planned.
|
October 18, 2005 Re: More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote:
> Deewiant wrote:
>
>> Property function calling syntax works with any function. For instance:
>>
>> writefln = "asdf";
>>
>> is the same as
>>
>> writefln("asdf");
>>
>> Unfortunately this cannot be combined with the array function calling to create
>> something like:
>>
>> char[] aeou(char[] a, char[] b);
>>
>> writefln = ("foo".aeou = "bar");
>>
>> Which is such a shame <g>
>
>
> WOW! I didn't know this works.
>
> int sq(int x){return x*x;}
> int y = 5;
>
> y = (sq = y);
> writefln(y);
>
> //y is 25;
>
> But this looks like a bug as i don't remember seeing this in the doc.
It's documented .. but only for classes!
It's just like
y = y.sq;
In the sense that, it's just another (fancy) way to call a function. i.e.
obj.func();
actually passes the address of "obj" to "func()".
obj.func = val;
is documented for classes, as being "properties".
|
October 31, 2005 Re: More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | Jari-Matti Mäkelä wrote:
> Ivan Senji wrote:
>
>> WOW! I didn't know this works.
>>
>> int sq(int x){return x*x;}
>> int y = 5;
>>
>> y = (sq = y);
>
>
> Now this looks terrible! I'm sure this is not what Walter had originally planned.
There was a long discussion before this got implemented. I for one wasn't entirely convinced, but hey, it's not my language. :-)
Anyhow, since this (definitely) is not intuitive for those learning the language (whether newbies or 20+ year C++ programmers), I suggest this should get a prominent place in the documentation.
Or else (ha, a threat!), I'll have to write a book about D gotchas, a la Scott Meyers! ;-)
---
Weighing the cost in wasted programmer hours learning the D language against the few minutes this gets handy for D gurus, I see now way to vote for this.
|
October 31, 2005 Re: More undocumented functionality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Georg Wrede | Georg Wrede wrote: > Jari-Matti Mäkelä wrote: > >> Ivan Senji wrote: >> >>> WOW! I didn't know this works. >>> >>> int sq(int x){return x*x;} >>> int y = 5; >>> >>> y = (sq = y); >> >> >> >> Now this looks terrible! I'm sure this is not what Walter had originally planned. > > > There was a long discussion before this got implemented. I for one wasn't entirely convinced, but hey, it's not my language. :-) > When exactly was this discussion? Some links please? I can't remember this strange feature being talked about. I can live with method's being invoked as properties (although it also has some isues) but non-member methods being called in property style? Why? When i think again it maybe could be usefull sometimes: void resX(int x) { //do something complicated to change resolution } and use it like this: resX = 1024; Might be cool sometimes :) > Anyhow, since this (definitely) is not intuitive for those learning the language (whether newbies or 20+ year C++ programmers), I suggest this should get a prominent place in the documentation. > > Or else (ha, a threat!), I'll have to write a book about D gotchas, a la Scott Meyers! ;-) > > --- > > Weighing the cost in wasted programmer hours learning the D language against the few minutes this gets handy for D gurus, I see now way to vote for this. |
Copyright © 1999-2021 by the D Language Foundation