object oriented value type
Robert Fraser
fraserofthenight at gmail.com
Tue Jun 26 00:01:41 PDT 2007
Interesting, interesting... There are more than a few places I've used a struct to wrap a single primitive value/enum so that I could make it typesafe and add methods to it, so I guess that isn't too fundamentally different, though it's much cleaner.
Still, the advantage of giving array-function-style syntax can be seen when it's used on a literal. Consider something like:
void times(int n, void delegate() action)
{
for(int i = 0; i < n; i++)
action();
}
3.times({writefln("Why, hello there!");});
...or, perhaps more useful
Very Ruby-esque, but clean in its own way. It reads quite like English, actually. Well, except for all those funky brackets & semicolons...
BCS Wrote:
> how about
>
> |typedef int myInt
> |{
> | static addCount=0; // static members? why not?
> |
> | myInt opAdd(myInt that)
> | {
> | addCount++;
> | return this+that; // this is int
> | }
> | private opMod(); // forbid mod on myInt
> |}
>
> added in template typedefs:
>
> |typedef real SIuint(int dist, int mass, int time)
> |{
> | SIuint!(dist+T.dist, mass+T.mass, time+T.time) opMull(T)(T p) // in-lining
> reduces to same as normal mul
> | {
> | return this*p;
> | }
> |}
>
> and some really cool stuff starts happening
>
>
More information about the Digitalmars-d
mailing list