equivariant functions
Simen Kjaeraas
simen.kjaras at gmail.com
Tue Oct 14 14:43:26 PDT 2008
On Tue, 14 Oct 2008 21:30:47 +0200, Denis Koroskin <2korden at gmail.com>
wrote:
> On Tue, 14 Oct 2008 23:12:43 +0400, Simen Kjaeraas
> <simen.kjaras at gmail.com> wrote:
>
>> On Tue, 14 Oct 2008 20:57:56 +0200, Denis Koroskin <2korden at gmail.com>
>> wrote:
>>
>>> On Tue, 14 Oct 2008 22:38:58 +0400, Simen Kjaeraas
>>> <simen.kjaras at gmail.com> wrote:
>>>
>>>> Also, this means a maximum of one
>>>> inout argument type per function, right?
>>>>
>>>
>>> No, there were many example of multiple (and even none) inout function
>>> arguments.
>>
>> I said argument types, as inout(MyClass) in inout foo(inout(MyClass) a,
>> inout(MyClass) b).
>>
>
> Isn't the following example good enough?
> inout(A) min(inout(A1) a1, inout(A2) a2); // 3 different types
It is, now that I reread it, and see that it's not all A's. :p
>>>> As for the keyword, I feel inout is better than typeof, and good
>>>> enough. Not perfect, but good enough.
>>>>
>>>
>>> My concern is that it than 'inout' doesn't express that all the
>>> arguments marked as inout as bound to each other and actual type is
>>> deduced from them all. There is a relationship between their types.
>>>
>>> inout(A) min(inout(A1) a1, inout(A2) a2);
>>>
>>> A1 a1;
>>> A2 a2;
>>>
>>> const(A1) ca1;
>>> const(A2) ca2;
>>>
>>> invariant(A1) ia1;
>>> invariant(A1) ia2;
>>>
>>> auto a = min(a1, ia2); // typeof(a) == const(A) even though neither a1
>>> nor ia2 of that type
>>> auto a = min(a1, a2); // typeof(a) == A
>>>
>>> You see, in the example above the return type is changed because type
>>> of 2nd argument is changed. Not only the return type but types of all
>>> the arguments, too.
>>>
>>> auto a = min(ia1, ia2); // typeof(a) == invariant(A). Now return type
>>> is changed per 1st argument type change.
>>
>> I don't really see this as a problem. Returning mutable or invariant
>> would be worse. Anyways, if you need the two arguments to be of the
>> same type, I'd prefer this syntax:
>>
>> inout min(inout(A) a1, typeof(a1) a2){}
>>
>
> Ouch! This one hurts. Yet it is inconsistent with multiple argument
> types.
With my newfound knowledge of this working on multiple argument types, I
agree.
> Besides, what advantages of it do you see over "inout min(inout(A) a1,
> inout(A) a2);"?
Mainly that it says "the type of a2 is the same as the type of a1",
while inout says "the return type is somehow dependant upon the types
of a1 and a2" (probably the lowest common denominator). If I feed mutable
and invariant data into a function, and can receive either of those back,
I expect it to be const. If I want to feed data of the same type/
constancy to a function, and want it to inform me if I'm doing it wrong,
I would state that they should be of the same type/constancy, not that
the return value is dependant upon both of them.
--
Simen
More information about the Digitalmars-d
mailing list