www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Min/max operators in D

reply Marti <integor gmail.com> writes:
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
Oct 28 2004
parent reply Sean Kelly <sean f4.ca> writes:
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
Oct 28 2004
next sibling parent reply Thomas Kuehne <eisvogel users.sourceforge.net> writes:
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
Oct 28 2004
parent reply Ant <Ant_member pathlink.com> writes:
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?
Oct 28 2004
parent Markus Dangl <danglm in.tum.de> writes:
 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>
Dec 28 2004
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Oct 29 2004
parent Sean Kelly <sean f4.ca> writes:
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
Oct 29 2004