www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Undocumented string functionality?

reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak invalid_utu.fi> writes:
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?
Oct 18 2005
next sibling parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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);
Oct 18 2005
prev sibling parent reply Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
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>
Oct 18 2005
next sibling parent BCS <BCS_member pathlink.com> writes:
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)
Oct 18 2005
prev sibling next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
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>

Oct 18 2005
prev sibling parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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.
Oct 18 2005
next sibling parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak invalid_utu.fi> writes:
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.
Oct 18 2005
parent reply Georg Wrede <georg.wrede nospam.org> writes:
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.
Oct 30 2005
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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.

Oct 30 2005
parent Derek Parnell <derek psych.ward> writes:
On Mon, 31 Oct 2005 08:25:06 +0100, Ivan Senji wrote:

 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?

Such methods are members, just not of classes or structs. They are members of the module that contains them. You can think of a module as a type of singleton object in that it contains its own methods and data but you can't derive new instances of them and you can't derive other modules from them.
 void resX(int x)
 {
 //do something complicated to change resolution
 }
 
 and use it like this:
 resX = 1024;

In your example (assuming its in a file called screen_io.d) you can say screen_io.resX = 1024; just like a class instance reference. Another neat usage is similar to your idea. Imagine that initially you had a module-level variable called 'resX' and you built you application around that idea. Then later, you realized that it needs some sophistication. You change it into a property and (most) of your code doesn't need changing. You still get stuffed up by lvalue situations... someFunc( &resX ); // Fails with properties! Example: import std.stdio; // Original code. version(original) { int resX; } // Enhanced code version(enhanced) { int actualRes; void resX(int x) { if ( x > 1024 ) actualRes = 1024; else if ( x > 0 ) actualRes = x; else actualRes = 0; } int resX() { return actualRes; } } void main() { screen_io.resX = 1024; writefln("Resolution is %d", resX); screen_io.resX = 1023; writefln("Resolution is %d", resX); screen_io.resX = 1025; writefln("Resolution is %d", resX); screen_io.resX = 0; writefln("Resolution is %d", resX); screen_io.resX = -21; writefln("Resolution is ", resX); } -- Derek (skype: derek.j.parnell) Melbourne, Australia 31/10/2005 6:27:07 PM
Oct 30 2005
prev sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
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".
Oct 18 2005