digitalmars.D - MIN MAX problem
- freeagle (23/23) Mar 12 2007 Hey all,
- torhu (6/17) Mar 13 2007 You can use typeof to get the common type, like this:
- freeagle (5/24) Mar 12 2007 havent seen in the docs something like type adding ?! anyway, my guess
- BCS (3/30) Mar 13 2007 The type of every function is fixed at compile time, It can't return a f...
- Walter Bright (6/12) Mar 13 2007 The + isn't always defined, so you can use:
- freeagle (5/19) Mar 12 2007 could you please explain what this means? coz i'd say it always return...
- Daniel Keep (15/36) Mar 13 2007 In the archives somewhere, there is a long thread discussing an
- Hasan Aljudy (2/7) Mar 13 2007 Doesn't the new mixin construct solve the issue?
- janderson (5/16) Mar 13 2007 Not unless you want to wrap your min/max statement's in mixins. I think...
- freeagle (3/39) Mar 13 2007 aha, sorry, i wasnt really followig the development and talk about D for...
- Andrei Alexandrescu (See Website For Email) (7/21) Mar 13 2007 Is there a plan to allow:
- Walter Bright (2/24) Mar 14 2007 That should probably be made to work, too, but currently it doesn't.
- =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= (3/13) Mar 14 2007 Is it impossibly hard or ambiguous to allow the compiler to infer the
- janderson (5/35) Mar 13 2007 In C++ you can use some copy constructor / assignment magic with a proxy...
Hey all, from what I know, there is (currently?) no way of implementing min/max template functions that could dynamically change return type depending on the result of comparison. I think that this problem can be "solved" easily. When used in C++, there is no problem with the return type as min/max are coded as macros. But there is always probability that the result of the min/max will be cast into appropriate type, because variables cant be both float and int at the same time, for example. That means, when you write in C++ float f = 1.0f, result; int a = 2; result = MAX(f, a); the result is cast to float when assignment to the variable is being done. This means you always know of what type you want the result to be. I say we could move the casting into the function itself, like this: T max(T, U)(T arg1, U arg2) { return cast(T) (arg1 > arg2 ? arg1 : arg2); } and make it a standard that the type of the first argument will always be the returning type of the min/max function. Just a thought freeagle
Mar 12 2007
freeagle wrote:the result is cast to float when assignment to the variable is being done. This means you always know of what type you want the result to be. I say we could move the casting into the function itself, like this: T max(T, U)(T arg1, U arg2) { return cast(T) (arg1 > arg2 ? arg1 : arg2); } and make it a standard that the type of the first argument will always be the returning type of the min/max function.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; }
Mar 13 2007
torhu wrote:freeagle wrote:havent seen in the docs something like type adding ?! anyway, my guess is that the typeof(T + U) expresion always return the the same type for each couple of types. so float + int would always return float? what if you want it to return int?the result is cast to float when assignment to the variable is being done. This means you always know of what type you want the result to be. I say we could move the casting into the function itself, like this: T max(T, U)(T arg1, U arg2) { return cast(T) (arg1 > arg2 ? arg1 : arg2); } and make it a standard that the type of the first argument will always be the returning type of the min/max function.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; }
Mar 12 2007
Reply to freeagle,torhu wrote:The type of every function is fixed at compile time, It can't return a float sometimes and an int others.freeagle wrote:havent seen in the docs something like type adding ?! anyway, my guess is that the typeof(T + U) expresion always return the the same type for each couple of types. so float + int would always return float? what if you want it to return int?the result is cast to float when assignment to the variable is being done. This means you always know of what type you want the result to be. I say we could move the casting into the function itself, like this: T max(T, U)(T arg1, U arg2) { return cast(T) (arg1 > arg2 ? arg1 : arg2); } and make it a standard that the type of the first argument will always be the returning type of the min/max function.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; }
Mar 13 2007
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; }
Mar 13 2007
Walter Bright wrote:torhu wrote: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?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; }
Mar 12 2007
freeagle wrote:Walter Bright 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!". 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 -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/torhu wrote: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?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; }
Mar 13 2007
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?
Mar 13 2007
Hasan Aljudy wrote:Daniel Keep wrote: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. -JoelIn 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?
Mar 13 2007
Daniel Keep wrote:freeagle wrote:aha, sorry, i wasnt really followig the development and talk about D for 2 or 3 months, so missed itWalter Bright 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!". 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 -- Danieltorhu wrote: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?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; }
Mar 13 2007
Walter Bright wrote:torhu wrote:Is there a plan to allow: typeof(true ? x1 : x2) max(T, U)(T x1, U x2) { return x1 > x2 ? x1 : x2; } AndreiYou 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; }
Mar 13 2007
Andrei Alexandrescu (See Website For Email) wrote:Walter Bright wrote:That should probably be made to work, too, but currently it doesn't.torhu wrote:Is there a plan to allow: typeof(true ? x1 : x2) max(T, U)(T x1, U x2) { return x1 > x2 ? x1 : x2; }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; }
Mar 14 2007
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 likeauto max(T, U)(T x1, U x2) { return x1 > x2 ? x1 : x2; }
Mar 14 2007
freeagle wrote:Hey all, from what I know, there is (currently?) no way of implementing min/max template functions that could dynamically change return type depending on the result of comparison. I think that this problem can be "solved" easily. When used in C++, there is no problem with the return type as min/max are coded as macros. But there is always probability that the result of the min/max will be cast into appropriate type, because variables cant be both float and int at the same time, for example. That means, when you write in C++ float f = 1.0f, result; int a = 2; result = MAX(f, a); the result is cast to float when assignment to the variable is being done. This means you always know of what type you want the result to be. I say we could move the casting into the function itself, like this: T max(T, U)(T arg1, U arg2) { return cast(T) (arg1 > arg2 ? arg1 : arg2); } and make it a standard that the type of the first argument will always be the returning type of the min/max function. Just a thought freeagleIn C++ you can use some copy constructor / assignment magic with a proxy object to get the task done. That technique won't handle different types in the same query though. -Joel
Mar 13 2007