www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - concept of "external methods" for integral and array types

reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
As mentioned before this works
"foobar".like("foo*");

signature of function is
bit like(char[] text, char[] pattern) {  }

But this is also works:

"%s\n".writef(b?"passed":"failed");

signature of function is void writef(...) {  }

First example works and makes sense, second is a bit doubtfull ...

As far as I understand it is a bug, but...
I've found it interesting as a concept of "external methods" for objects of 
integral types and arrays.

I think that they have all righths for existence:

alias char[] string;

string  int.toString() { return ...; }
string  string.like(string pattern) { return ...; }

....

I cannot see any principal problems with them. My notation of external 
methods declaration probably is not perfect but
from grammar point of view it is noncontradictory ( I've not analyzed this 
deeply though).

As far as I can see this will allow to add a string (pseudo)object in pretty 
elegant D-ish way.
Also ability to add methods to int, real, etc would be nice - sometimes they 
definitely needed.

What do you guys think?

Andrew Fedoniouk.
http://terrainformatica.com
Feb 11 2005
parent reply Norbert Nemec <Norbert Nemec-online.de> writes:
Andrew Fedoniouk wrote:

 As mentioned before this works
 "foobar".like("foo*");
 
 signature of function is
 bit like(char[] text, char[] pattern) {  }
 
 But this is also works:
 
 "%s\n".writef(b?"passed":"failed");
 
 signature of function is void writef(...) {  }
 
 First example works and makes sense, second is a bit doubtfull ...
 
 As far as I understand it is a bug, but...
Why 'bug'? It follows straightforwardly from the specs. Of course, one can question whether it is good style, but I don't see any problem with it.
 I've found it interesting as a concept of "external methods" for objects
 of integral types and arrays.
 
 I think that they have all righths for existence:
 
 alias char[] string;
 
 string  int.toString() { return ...; }
 string  string.like(string pattern) { return ...; }
So, what's the point of it? As you said, you can already define: string like(string a, string pattern) { return ...; } and call it like "foobar".like("foo*"); What would be the semantic difference of your definition?
 I cannot see any principal problems with them. My notation of external
 methods declaration probably is not perfect but
 from grammar point of view it is noncontradictory ( I've not analyzed this
 deeply though).
I fear it would complicate parsing quite a bit, but that's for others to investigate.
Feb 12 2005
next sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Norbert Nemec" <Norbert Nemec-online.de> wrote in message
news:cukftn$ut6$1 digitaldaemon.com...
 Andrew Fedoniouk wrote:

 As mentioned before this works
 "foobar".like("foo*");

 signature of function is
 bit like(char[] text, char[] pattern) {  }

 But this is also works:

 "%s\n".writef(b?"passed":"failed");

 signature of function is void writef(...) {  }

 First example works and makes sense, second is a bit doubtfull ...

 As far as I understand it is a bug, but...
Why 'bug'? It follows straightforwardly from the specs.
Did you actually find it in spec? I looked and looked but can't find it. <snip>
Feb 12 2005
prev sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 Why 'bug'? It follows straightforwardly from the specs. Of course, one can
 question whether it is good style, but I don't see any problem with it.
Norbert, seems like we are using different documents.... Is this http://www.digitalmars.com/d/function.html what you are talking about? Andrew Fedoniouk. http://terrainformatica.com "Norbert Nemec" <Norbert Nemec-online.de> wrote in message news:cukftn$ut6$1 digitaldaemon.com...
 Andrew Fedoniouk wrote:

 As mentioned before this works
 "foobar".like("foo*");

 signature of function is
 bit like(char[] text, char[] pattern) {  }

 But this is also works:

 "%s\n".writef(b?"passed":"failed");

 signature of function is void writef(...) {  }

 First example works and makes sense, second is a bit doubtfull ...

 As far as I understand it is a bug, but...
Why 'bug'? It follows straightforwardly from the specs. Of course, one can question whether it is good style, but I don't see any problem with it.
 I've found it interesting as a concept of "external methods" for objects
 of integral types and arrays.

 I think that they have all righths for existence:

 alias char[] string;

 string  int.toString() { return ...; }
 string  string.like(string pattern) { return ...; }
So, what's the point of it? As you said, you can already define: string like(string a, string pattern) { return ...; } and call it like "foobar".like("foo*"); What would be the semantic difference of your definition?
 I cannot see any principal problems with them. My notation of external
 methods declaration probably is not perfect but
 from grammar point of view it is noncontradictory ( I've not analyzed 
 this
 deeply though).
I fear it would complicate parsing quite a bit, but that's for others to investigate.
Feb 12 2005