www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variant opArithmetic

reply "cal" <callumenator gmail.com> writes:
Variant tries to mimic D's built-in rules for arithmetic 
conversions but:

import std.variant, std.stdio;
void main()
{
   auto v1 = Variant(4.5f);
   auto v2 = Variant(3.5f);
   writeln((v1+v2).type()); // double
}

The reason is Variant doesn't try to convert to float in 
opArithmetic (it's is easily fixed). But is it intentional?
Sep 18 2012
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 9/18/12 1:36 PM, cal wrote:
 Variant tries to mimic D's built-in rules for arithmetic conversions but:

 import std.variant, std.stdio;
 void main()
 {
 auto v1 = Variant(4.5f);
 auto v2 = Variant(3.5f);
 writeln((v1+v2).type()); // double
 }

 The reason is Variant doesn't try to convert to float in opArithmetic
 (it's is easily fixed). But is it intentional?

I wrote this: writeln(typeof(4.5f + 3.5f).stringof); To my surprise, the reported type was float, not double. Well that means it's a bug in Variant, could you please make a bug report (and hopefully a pull request fixing it)? Thanks, Andrei
Sep 18 2012
prev sibling parent "cal" <callumenator gmail.com> writes:
On Tuesday, 18 September 2012 at 19:02:33 UTC, Andrei 
Alexandrescu wrote:
 On 9/18/12 1:36 PM, cal wrote:
 Variant tries to mimic D's built-in rules for arithmetic 
 conversions but:

 import std.variant, std.stdio;
 void main()
 {
 auto v1 = Variant(4.5f);
 auto v2 = Variant(3.5f);
 writeln((v1+v2).type()); // double
 }

 The reason is Variant doesn't try to convert to float in 
 opArithmetic
 (it's is easily fixed). But is it intentional?

I wrote this: writeln(typeof(4.5f + 3.5f).stringof); To my surprise, the reported type was float, not double. Well that means it's a bug in Variant, could you please make a bug report (and hopefully a pull request fixing it)? Thanks, Andrei

Sure thing
Sep 18 2012