March 14, 2007 Re: MIN MAX problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | Hasan Aljudy wrote:
>
>
> Daniel Keep wrote:
>>
>> In the archives somewhere, there is a long thread discussing an
>> implementation of min (or max; can't remember). From what I do
>> remember, it basically boiled down to "good grief this is actually
>> really hard once you involve types!".
>
> Doesn't the new mixin construct solve the issue?
>
Not unless you want to wrap your min/max statement's in mixins. I think this is overkill for min/max unless it was used for some higher level pattern.
-Joel
|
March 14, 2007 Re: MIN MAX problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep wrote:
> freeagle wrote:
>> Walter Bright wrote:
>>> torhu wrote:
>>>> You can use typeof to get the common type, like this:
>>>>
>>>> typeof(T + U) max(T, U)(T arg1, U arg2)
>>>> {
>>>> return arg1 > arg2 ? arg1 : arg2;
>>>> }
>>> The + isn't always defined, so you can use:
>>>
>>> typeof(true ? cast(T)0 : cast(U)0) max(T, U)(T x1, U x2)
>>> {
>>> return x1 > x2 ? x1 : x2;
>>> }
>> could you please explain what this means? coz i'd say it always returns T
>>
>> typeof(true ? cast(T)0 : cast(U)0)
>>
>> And, i think this wont work for classes that overload the >,< etc
>> operator, as you can't do cast(class)0, can you?
>
> In the archives somewhere, there is a long thread discussing an
> implementation of min (or max; can't remember). From what I do
> remember, it basically boiled down to "good grief this is actually
> really hard once you involve types!".
>
> Might be worth searching for it and having a read. In fact, because I'm
> feeling so helpful this morning:
>
> http://www.digitalmars.com/d/archives/digitalmars/D/challenge_implement_the_max_function_47026.html
>
> Hope this helps
>
> -- Daniel
>
aha, sorry, i wasnt really followig the development and talk about D for 2 or 3 months, so missed it
|
March 14, 2007 Re: MIN MAX problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu (See Website For Email) | Andrei Alexandrescu (See Website For Email) wrote:
> Walter Bright wrote:
>> torhu wrote:
>>> You can use typeof to get the common type, like this:
>>>
>>> typeof(T + U) max(T, U)(T arg1, U arg2)
>>> {
>>> return arg1 > arg2 ? arg1 : arg2;
>>> }
>>
>> The + isn't always defined, so you can use:
>>
>> typeof(true ? cast(T)0 : cast(U)0) max(T, U)(T x1, U x2)
>> {
>> return x1 > x2 ? x1 : x2;
>> }
>
> Is there a plan to allow:
>
> typeof(true ? x1 : x2) max(T, U)(T x1, U x2)
> {
> return x1 > x2 ? x1 : x2;
> }
That should probably be made to work, too, but currently it doesn't.
|
March 14, 2007 Re: MIN MAX problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu (See Website For Email) | Andrei Alexandrescu wrote: > Is there a plan to allow: > > typeof(true ? x1 : x2) max(T, U)(T x1, U x2) > { > return x1 > x2 ? x1 : x2; > } Is it impossibly hard or ambiguous to allow the compiler to infer the return type here? Something like > auto max(T, U)(T x1, U x2) > { > return x1 > x2 ? x1 : x2; > } |
Copyright © 1999-2021 by the D Language Foundation