View mode: basic / threaded / horizontal-split · Log in · Help
October 28, 2004
Min/max operators in D
I think having min/max operators built into the language would be useful.
A good example might be the GCC C++ min/max extension [1]:

a <? b
  is the minimum, returning the smaller of the numeric values a and b;

a >? b
  is the maximum, returning the larger of the numeric values a and b.

These operators can also be chained, eg. `1 >? 2 >? 3`, to compare more than
two variables.

Even though its implementation should take minimal resources and effort, I'm 
not sure if would pay off in terms of language/compiler complexity.

[1] http://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Min-and-Max.html
October 28, 2004
Re: Min/max operators in D
In article <clr0ja$2elt$1@digitaldaemon.com>, Marti says...
>
>I think having min/max operators built into the language would be useful.

It's a pretty cool idea, especially in the absence of macros.  A standard
template implementation would have to be called as:

std.min!(int,float)(x,y);

Which seems kind of verbose and a tad brittle.

Sean
October 28, 2004
Re: Min/max operators in D
In article <clr37c$2het$1@digitaldaemon.com>, Sean Kelly says...
>
>In article <clr0ja$2elt$1@digitaldaemon.com>, Marti says...
>>
>>I think having min/max operators built into the language would be useful.
>
>It's a pretty cool idea, especially in the absence of macros.  A standard
>template implementation would have to be called as:
>
>std.min!(int,float)(x,y);
>
>Which seems kind of verbose and a tad brittle.

This would be an extreamly usefull extentsion.

While we are extending, how about a typesafe "swap" opperator for the current
code: 
# tmp = a;
# a = b; 
# b = a;

Thomas
October 28, 2004
Re: Min/max operators in D
In article <clr54d$2jm6$1@digitaldaemon.com>, Thomas Kuehne says...
>
>In article <clr37c$2het$1@digitaldaemon.com>, Sean Kelly says...
>>
>>In article <clr0ja$2elt$1@digitaldaemon.com>, Marti says...
>>>
>>>I think having min/max operators built into the language would be useful.
>>
>>It's a pretty cool idea, especially in the absence of macros.  A standard
>>template implementation would have to be called as:
>>
>>std.min!(int,float)(x,y);
>>
>>Which seems kind of verbose and a tad brittle.
>
>This would be an extreamly usefull extentsion.
>
>While we are extending, how about a typesafe "swap" opperator for the current
>code: 
># tmp = a;
># a = b; 
># b = a;
>

what's your problem? can't you do:
a = b + ((b=a)-a);

(it's a joke!, it's a joke!
must resist posting..., must resist posting...,  oops, to late ;)

Ant
PS this should work as consistently as i = i++;
BTW is i = i++; defined in D?
October 29, 2004
Re: Min/max operators in D
Sean Kelly wrote:
> In article <clr0ja$2elt$1@digitaldaemon.com>, Marti says...
> 
>> I think having min/max operators built into the language would be useful.
> 
> It's a pretty cool idea, especially in the absence of macros.  A standard
> template implementation would have to be called as:
> 
> std.min!(int,float)(x,y);

And what would be the return type of such a function?

I'd think a one-parameter template would suffice....

Stewart.
October 29, 2004
Re: Min/max operators in D
Stewart Gordon wrote:
> Sean Kelly wrote:
> 
>> In article <clr0ja$2elt$1@digitaldaemon.com>, Marti says...
>>
>>> I think having min/max operators built into the language would be 
>>> useful.
>>
>>
>> It's a pretty cool idea, especially in the absence of macros.  A standard
>> template implementation would have to be called as:
>>
>> std.min!(int,float)(x,y);
> 
> And what would be the return type of such a function?

Oops, good point.  There are two-type versions in C++, but they 
typically rely on implicit template instantiation and tend to be quite 
complicated.

> I'd think a one-parameter template would suffice....

It would, though still not as well as a macro.  But perhaps it isn't as 
much of an issue as in C++ since D doesn't have implicit template 
instantiation.  I can think of times in C++ code where the result type 
I'd want to choose would not be very clear, but I can't see this 
happening in D since all types must be specified by the programmer.


Sean
December 29, 2004
Re: Min/max operators in D
> what's your problem? can't you do:
> a = b + ((b=a)-a);
> 
> (it's a joke!, it's a joke!
> must resist posting..., must resist posting...,  oops, to late ;)
> 

<joke>
a=b-a;b-=a;a+=b; ???
</joke>
Top | Discussion index | About this forum | D home