digitalmars.D.learn - Undocumented string functionality?
- =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= (12/12) Oct 18 2005 I tried to split a few lines and found this handy (?) functionality. It
- Ivan Senji (14/30) Oct 18 2005 Yes it can.
- Deewiant (9/9) Oct 18 2005 Property function calling syntax works with any function. For instance:
- BCS (8/8) Oct 18 2005 IIRC the general case is as this:
- Unknown W. Brackets (3/19) Oct 18 2005 Hmm.... I must say I would see this one (fcall = param) being a source
- Ivan Senji (8/24) Oct 18 2005 WOW! I didn't know this works.
- =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= (3/9) Oct 18 2005 Now this looks terrible! I'm sure this is not what Walter had originally...
- Georg Wrede (12/24) Oct 30 2005 There was a long discussion before this got implemented. I for one
- Ivan Senji (13/45) Oct 30 2005 When exactly was this discussion? Some links please? I can't remember
- Derek Parnell (54/64) Oct 30 2005 Such methods are members, just not of classes or structs. They are membe...
- Hasan Aljudy (9/41) Oct 18 2005 It's documented .. but only for classes!
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
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
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
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
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
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
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
Jari-Matti Mäkelä wrote:Ivan Senji wrote: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.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 30 2005
Georg Wrede wrote:Jari-Matti Mäkelä wrote: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 :)Ivan Senji wrote:There was a long discussion before this got implemented. I for one wasn't entirely convinced, but hey, it's not my language. :-)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.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
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
Ivan Senji wrote:Deewiant wrote: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".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