www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I have a dream about external methods too ...

reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
About external methods (or multimethods?)....

I would like to be able to write something like this one day....
Would be extremely nice....

typedef wchar[] string
{
    void insert(int at, wchar what)
    {
        int num = this.length;
        this.length = num + 1;
        ......
    }
    void opCatAssign(wchar what)
    {
        this.length = this.length + 1;
        this[this.length - 1]  = what;
     }
     ....
}

typedef uint color
{
    uint red()  {  return this & 0xFF; }
    uint green()  { ... }
    uint blue()  { ... }
}

Huh?

Andrew. 
Mar 13 2005
next sibling parent reply pragma <pragma_member pathlink.com> writes:
In article <d136o4$mbg$1 digitaldaemon.com>, Andrew Fedoniouk says...
About external methods (or multimethods?)....

I would like to be able to write something like this one day....
Would be extremely nice....

typedef wchar[] string
{
    void insert(int at, wchar what)
    {
        int num = this.length;
        this.length = num + 1;
        ......
    }
    void opCatAssign(wchar what)
    {
        this.length = this.length + 1;
        this[this.length - 1]  = what;
     }
     ....
}

typedef uint color
{
    uint red()  {  return this & 0xFF; }
    uint green()  { ... }
    uint blue()  { ... }
}

Huh?

I couldn't agree more. In fact, I proposed almost the same exact thing a (long) while back. (sorry if that was what you were referring to) ;) OP: digitalmars.D/8159 Proposal: http://www.prowiki.org/wiki4d/wiki.cgi?Typedef-Block - EricAnderton at yahoo
Mar 13 2005
next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
:)

I have not seen your post, Eric! Honestly!

If two wise men (yet decent :)
independently (sic!) come up to the same idea
then it means that this idea at least is worth
something :)

Andrew.


"pragma" <pragma_member pathlink.com> wrote in message 
news:d1380p$o27$1 digitaldaemon.com...
 In article <d136o4$mbg$1 digitaldaemon.com>, Andrew Fedoniouk says...
About external methods (or multimethods?)....

I would like to be able to write something like this one day....
Would be extremely nice....

typedef wchar[] string
{
    void insert(int at, wchar what)
    {
        int num = this.length;
        this.length = num + 1;
        ......
    }
    void opCatAssign(wchar what)
    {
        this.length = this.length + 1;
        this[this.length - 1]  = what;
     }
     ....
}

typedef uint color
{
    uint red()  {  return this & 0xFF; }
    uint green()  { ... }
    uint blue()  { ... }
}

Huh?

I couldn't agree more. In fact, I proposed almost the same exact thing a (long) while back. (sorry if that was what you were referring to) ;) OP: digitalmars.D/8159 Proposal: http://www.prowiki.org/wiki4d/wiki.cgi?Typedef-Block - EricAnderton at yahoo

Mar 13 2005
prev sibling next sibling parent reply Ben Hinkle <Ben_member pathlink.com> writes:
OP:
digitalmars.D/8159

Proposal:
http://www.prowiki.org/wiki4d/wiki.cgi?Typedef-Block

nice write-up! That's a great use of wiki. I like this external method stuff. It would make extending basic types easier. A variation on all this would be something that makes official the array-method hack: return-type fcnname(T[], ..) can be called like x.fcnname(...). The general form of this would be return-type fcnname(T, ...) can be called with x.fcnname(...). Maybe a concrete proposal for this has been suggested already, too? For example, # char[] toString(int x){...} # ... # int y; # printf("%.*s",y.toString()); // no need for .toString The name lookup and overloading rules would have to be massaged so that fcnname(T,...) could be found. I'll ponder this some more and probably try to write up as nice a wiki proposal as you did for the typedef extension. -Ben
Mar 14 2005
parent pragma <pragma_member pathlink.com> writes:
In article <d14403$1lbo$1 digitaldaemon.com>, Ben Hinkle says...
OP:
digitalmars.D/8159

Proposal:
http://www.prowiki.org/wiki4d/wiki.cgi?Typedef-Block

nice write-up! That's a great use of wiki.

Thank you. I plan on doing this more often in the future, as per my suggestions for improving the DNG. Please, post any critiques you have to wiki if you feel like it. Its not captured on the page, but a major motivation I have for this is to provide a way to overload operators for scalars. Right now, that's and advantage that C++ has over D.
 I like this external method stuff. It
would make extending basic types easier. A variation on all this would be
something that makes official the array-method hack

That gets my vote too, provided that operator overloads can be worked-in. I prefer the typedef-block solution only because it forces one to create a new type, so the built-in primitives can stay as-is. I think that's important, as I can easily see operator overloads on the built-in types as a font of bugs. - EricAnderton at yahoo
Mar 14 2005
prev sibling next sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
 Proposal:
 http://www.prowiki.org/wiki4d/wiki.cgi?Typedef-Block

How do I get to the proposal(s) from the FrontPage of the wiki?
Mar 14 2005
parent pragma <pragma_member pathlink.com> writes:
In article <d14bbd$1tok$1 digitaldaemon.com>, Ben Hinkle says...
 Proposal:
 http://www.prowiki.org/wiki4d/wiki.cgi?Typedef-Block

How do I get to the proposal(s) from the FrontPage of the wiki?

The closest thing we have is the Feature Request List, which is anything but a stack of formal proposals. http://www.prowiki.org/wiki4d/wiki.cgi?FeatureRequestList .. and it's not linked directly from the front page, but is available via "Idea Discussion". - EricAnderton at yahoo
Mar 14 2005
prev sibling parent Sean Kelly <sean f4.ca> writes:
In article <d1380p$o27$1 digitaldaemon.com>, pragma says...
I couldn't agree more.  In fact, I proposed almost the same exact thing a (long)
while back. (sorry if that was what you were referring to) ;)

OP:
digitalmars.D/8159

Proposal:
http://www.prowiki.org/wiki4d/wiki.cgi?Typedef-Block

I missed this the first time around. It's an intruging idea, particularly in its ability to extend the functionality of primitive types. Assuming it wouldn't be terribly difficult to implement, I'm all for it. Sean
Mar 14 2005
prev sibling next sibling parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
Well, I thought it could easily be done by just making a method that takes a 
'color' but then calling it like some_color.red() instead of 
red(some_color)... But it didn't work:

The method 'toupper' can be called as string.toupper(), but why can't the 
same be done for 'color' ? Even without the typedef (using uint) it won't 
work?

Lionello


void toupper( char[] c )
{
 c ~= "bla";
}

typedef uint color;
int red( color c ) { return c&0xFF; }

int main( char[][]arg )
{
 char[] string;
 string.toupper();

 uint c;
 return c.red();
}
Mar 14 2005
parent reply Sean Kelly <sean f4.ca> writes:
In article <d13ggf$10rn$1 digitaldaemon.com>, Lionello Lunesu says...
Well, I thought it could easily be done by just making a method that takes a 
'color' but then calling it like some_color.red() instead of 
red(some_color)... But it didn't work:

The method 'toupper' can be called as string.toupper(), but why can't the 
same be done for 'color' ? Even without the typedef (using uint) it won't 
work?

Sadly, this trick only works for arrays. I would love to see it enabled for primitive types as well. Sean
Mar 14 2005
parent reply "Charles" <cee-lo green.com> writes:
 Sadly, this trick only works for arrays.  I would love to see it enabled

 primitive types as well.

Aye me too. Would this eliminate the need for multi-methods ? Charlie "Sean Kelly" <sean f4.ca> wrote in message news:d14u8u$2hks$1 digitaldaemon.com...
 In article <d13ggf$10rn$1 digitaldaemon.com>, Lionello Lunesu says...
Well, I thought it could easily be done by just making a method that


'color' but then calling it like some_color.red() instead of
red(some_color)... But it didn't work:

The method 'toupper' can be called as string.toupper(), but why can't the
same be done for 'color' ? Even without the typedef (using uint) it won't
work?

Sadly, this trick only works for arrays. I would love to see it enabled

 primitive types as well.


 Sean

Mar 14 2005
next sibling parent Sean Kelly <sean f4.ca> writes:
In article <d14vgd$2j03$1 digitaldaemon.com>, Charles says...
 Sadly, this trick only works for arrays.  I would love to see it enabled

 primitive types as well.

Aye me too. Would this eliminate the need for multi-methods ?

If you mean the proposed typedef block trick, then no. But consistency is generally a good thing, and this could have an interesting impact on template code. Sean
Mar 14 2005
prev sibling parent reply "Craig Black" <cblack ara.com> writes:
 Aye me too.  Would this eliminate the need for multi-methods ?

No. Here you are essentially extending a class or type similar to C#'s partial classes. This is not a multimethod, nor does it replace their functionality. Multimethods are an elegant way to perform multiple dispatch. They define relationships between objects. Where virtual methods use 1-dimensional vtables, multimethods are N-dimensional method tables, because multiple parameters in a multimethod can be "virtual". If you would like to know more about multimethods, just ask. -Craig
Mar 14 2005
parent "Charles" <cee-lo green.com> writes:
 If you would like to know more about multimethods, just ask.

I would :). Been trying to find a good resource on the web, have a link ? Charlie "Craig Black" <cblack ara.com> wrote in message news:d151j0$2l37$1 digitaldaemon.com...
 Aye me too.  Would this eliminate the need for multi-methods ?

No. Here you are essentially extending a class or type similar to C#'s partial classes. This is not a multimethod, nor does it replace their functionality. Multimethods are an elegant way to perform multiple dispatch. They define relationships between objects. Where virtual

 use 1-dimensional vtables, multimethods are N-dimensional method tables,
 because multiple parameters in a multimethod can be "virtual".

 If you would like to know more about multimethods, just ask.

 -Craig

Mar 14 2005
prev sibling next sibling parent "Joey Peters" <squirrel nidhogg.com> writes:
That somehow looks strange and unlikely, but useful. It could work a little 
different, much like how you can do:

enum Something : int { ... }

Perhaps one should be able to do:

# class Something : char[] {
#   void insert(int at, wchat what) { super.length = super.length + 1; ... }
# }

But it just doesn't make sense somehow.

"Andrew Fedoniouk" <news terrainformatica.com> schreef in bericht 
news:d136o4$mbg$1 digitaldaemon.com...
 About external methods (or multimethods?)....

 I would like to be able to write something like this one day....
 Would be extremely nice....

 typedef wchar[] string
 {
    void insert(int at, wchar what)
    {
        int num = this.length;
        this.length = num + 1;
        ......
    }
    void opCatAssign(wchar what)
    {
        this.length = this.length + 1;
        this[this.length - 1]  = what;
     }
     ....
 }

 typedef uint color
 {
    uint red()  {  return this & 0xFF; }
    uint green()  { ... }
    uint blue()  { ... }
 }

 Huh?

 Andrew.

Mar 14 2005
prev sibling parent "Craig Black" <cblack ara.com> writes:
 About external methods (or multimethods?)....

This is a great idea, but this is not "multimethods". -Craig
Mar 14 2005