generic + numeric + literals = abomination

so so at
Sat Mar 27 10:18:40 PDT 2010

Oh... wait a second.
In :

"Regardless of the type of the operands, floating point constant folding  
is done in real or greater precision. It is always done following IEEE 754  
rules and round-to-nearest is used.

Floating point constants are internally represented in the implementation  
in at least real precision, regardless of the constant's type. The extra  
precision is available for constant folding. Committing to the precision  
of the result is done as late as possible in the compilation process"

Wow! DMD already doing it, with or without literal!

With this in mind, just one thing bugs me.

import std.stdio;

struct vector(T) {
	this(T m) { mm = m; }
	vector!T opBinary(string s)(T m) if(s=="*") {
		return vector!T(mm * m);
	T mm;

void test(T)() {
	vector!T v = vector!T(0.5);
	vector!T u = v * 0.3;
	writeln("u: ",;

void main() {

This program compiles and runs just fine, but i feel dirty, you see I  
explicitly stated that opBinary takes a variable of type T,
but it accepted 0.3 on all 3 tests, implicitly casted double to T. In C++  
world this brings tons of trouble, especially performance problems,
but here i am not sure what DMD does that there :)


On Sat, 27 Mar 2010 16:44:32 +0200, bearophile <bearophileHUGS at>  

> so:
>> When i have the code :
>> scalar m = 0.5fp;
>> I want compiler to implicitly cast it to typeof(scalar).
>> so when the scalar is float, m will be 0.5f.
> I am starting to understand, sorry if I am a dumb bear :-) Programming  
> in C++/D is usually much less hard than understanding humans.
> I think you mean an universal FP literal that's automatically cast-able  
> to any other floating point type, in template arguments too.
> Are you able to write a 10 lines long D program that shows a  
> compile-time error that you don't want to happen? I am willing to fix  
> your code until it shows what you want to show me (assuming it's a real  
> problem in D).
> Bye,
> bearophile

Using Opera's revolutionary e-mail client:

More information about the Digitalmars-d-learn mailing list