opSlice and $

Witold Baryluk baryluk at smp.if.uj.edu.pl
Tue Sep 11 12:50:44 PDT 2007


Dnia Tue, 11 Sep 2007 21:27:44 +0200
Oskar Linde <oskar.lindeREM at OVEgmail.com> napisał/a:

> Sean Kelly wrote:
> > Russell Lewis wrote:
> >> It appears to me, looking at the spec, that there isn't a way to 
> >> support the use of $ in operator overloading.  Is there anything
> >> I've overlooked?  What I'm looking for, of course, is to be able
> >> to do:
> >>
> >> class MyClass {....}
> >>
> >> MyClass c = new MyClass;
> >> auto x = c[1..$];
> > 
> > The '$' symbol should map to "size_t length()" when used with
> > opSlice, but at the moment I don't think this is the case.
> 
> The problem how that generalizes to multidimensional slicing:
> c[1..$, 2..$]


Yes. It will be nice to have this. Currenly I use temporary class for
remebering first dimension of slice, and object from which we slice.

c[1..3][1..5]


It is also problem that you can only have size_t in opSlice
what about other types?

c[ "a" .. "b"]

or

c[ "names", "A" .. "Z" ]
c[ "numbers", 1 .. 9 ], 

or more complicated slices...

c[ "x", 2..5, "as" .. $,  $ .. $-1, 4, 5].
                      ^a  ^b

where a is different from b.


I dont know even what signature it will have, how to distinguish such
uses:
c[1, 2..3]
c[1..2,3]

mayby:
opSlice(int, SliceTuple!(int, int));
opSlice(SliceTuple!(int, int), int);

this then can be extended to things like:

c [ 1 .. 2 .. 3]        // SliceTuple!(int, int, int), ie. all paths
                        // from 1 to 3 throught 3

or
c [ 0 .. "9198231987397813" ]


but what about $'s ?


Not all of this is practical, but it will be good to have some
elastic syntax for it.


Creating temporary objects are very general, but why not have small
syntactic sugar ([ .. ] is also syntactic sugar for opSlice call) or
automatic creation of this objects and then rewriting this to:


c[ "x" ][ 2..5 ][ "as" .. $ ][ $ .. $-1 ][ 4 ][ 5 ].

now all is simple. Every prefix will acumulate every pair
and at the last call it will call some method.

It can be good excerise to write some very general template mechanism
for this.


Generating temporary object is also good for things like:

c[1..3][4..5] += b[1..3][4..5];

it is quite trivial when using temporary object (overload opAddAssign).
D have no opSlice*Assign (similary there is no opIndex*Assign).  :/


-- 
Witold Baryluk, aleph0
MAIL: baryluk at smp.if.uj.edu.pl
JID: movax at jabber.autocom.pl



More information about the Digitalmars-d mailing list