Extend vector ops to boolean operators?

James Miller james at aatch.net
Tue Mar 6 19:12:05 PST 2012


On 7 March 2012 15:35, Peter Alexander <peter.alexander.au at gmail.com> wrote:
> On Tuesday, 6 March 2012 at 23:57:07 UTC, James Miller wrote:
>>
>> On 7 March 2012 10:58, Kapps <opantm2+spam at gmail.com> wrote:
>>>
>>> On Tuesday, 6 March 2012 at 20:28:40 UTC, H. S. Teoh wrote:
>>>>
>>>>
>>>> It'd be really cool if I could do this:
>>>>
>>>>        void func(int[] vector, int[] bounds) {
>>>>                assert(vector[] >= 0 && vector[] < bounds[]);
>>>>
>>>>                ...
>>>>        }
>>>>
>>>> Is there any reason why we shouldn't implement this?
>>>>
>>>>
>>>> T
>>>
>>>
>>>
>>> Would this be possible with UFCS?
>>>
>>> int opCmp(T)T([] array, T element) { ... }
>>> int opCmp(T)(T[] array1, T[] array2) { ... }
>>
>>
>> I like this idea, at least adding an opSliceCmp operator-overload
>> would do as a start, I think thats the correct name for it. I can't be
>> bothered to check.
>>
>> --
>> James Miller
>
>
> It has to be done as vector operations.
>
> a[] < b[] should equal [a[0] < b[0], a[1] < b[1], ... ]
>
> What the OP has asked for is not a vector operation, so it shouldn't use the
> vector op syntax.

What? I'm assuming you mean that you expect an array of `bool`s? While
I agree that most vector operations should return a vector, comparison
makes more sense as returning a straight bool, most of the time you
aren't going to want to just have an array of bools. The only use case
i can think of is this:

auto c = a[] < b[]
foreach (i : c) {
    if (i) doSomething();
    else doSomethingElse();
}

Which can be easily re-written as

for (int i; i < a.length; i++) {
    if (a[i] < b[i]) doSomething();
    else doSomethingElse();
}

(ignoring things like proper checking etc.)

Of course most vector ops should return vectors, but most other ops
return proper types too, comparison ops tend to be the exception to
the rule.

--
James Miller


More information about the Digitalmars-d mailing list