A possible solution for the opIndexXxxAssign morass
Bill Baxter
wbaxter at gmail.com
Tue Oct 13 10:19:24 PDT 2009
On Tue, Oct 13, 2009 at 10:00 AM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> On Tue, 13 Oct 2009 12:44:21 -0400, Denis Koroskin <2korden at gmail.com>
> wrote:
>
>> Another thing I dislike about this proposal is that "a[b] += c;"
>> translates into "opAddAssign" and doesn't mention "index" while "a[b] = c;"
>> does ("opIndexAssign").
>
> I think the optimization translates to opAssign as well:
>
> a[b] = c; => a.opAssign(b, c);
>
> On Tue, 13 Oct 2009 12:37:50 -0400, Bill Baxter <wbaxter at gmail.com> wrote:
>
>> Huh? It didn't sound to me like it would get rid of anything, except
>> for the use of the word "index" in many methods that have to do with
>> index operations. That just seems confusing to me. I think the
>> opIndexXxxAssign functions may need to be added, but adding them by
>> overloading existing names doesn't seem a win to me.
>
> The point is to avoid having operator function names multiply out of
> control. Re-examining it, I agree with you -- It makes little sense to have
> an operator that involves an indexing lack the term Index.
>
> If only there was a way to make the indexing orthogonal to the other
> operation. For example something like:
>
> struct X
> {
> private int[] arr;
> opIndex(int idx) // declares a new "namespace" where idx is an implicitly
> passed argument
> {
> int opAssign(int x)
> {
> arr[idx] = x;
> return x;
> }
> }
> }
>
> I know this probably doesn't parse well, should opIndex be a keyword? or an
> attribute?
I don't think the number of /names/ required is the problem. It's
just the sheer number of functions themselves that's the issue. I
think a lot of that could mostly be fixed by some smart macros. And
until they exist, mixins can help.
struct Vec
{
float x,y,z;
mixin(implementOperators("+ - / * += -= /= *=",
q{
a.x op= b.x;
a.y op= b.y;
a.z op= b.z;
});
}
The code gives a list of operators to implement and one prototypical
op= body. With a smart enough CTFE string function that's all you
need to generate all the listed operators. Not sure how to work index
operators into that.
--bb
More information about the Digitalmars-d
mailing list