This works today, no (except for the d is redundant)? I guess I don't really understand what you are looking for...

-Steve

Could you please write the equivalent D code for generic inv function?
I guess it would be

T inv(T)(T m) {
        return 1 / m;
}

Right? With a glance, It is perfectly what i want!

Now lets apply C derived language (D is one of them as we all know) rules here and analyze what is happening.
Q - what is "1" ?
A - "1" is an "int", which means, "signed int", also in D it is "32 bit signed it", in C bit count is implementation defined, of course D way much better.
Q - what happens when i call inv(5.0f)
A - "return float(1) / 5.0f"
Anything wrong with questions and answers here?

What i am asking is why it have to be this way.
In "return 1 / float", why 1 is assumed as an integer, and consequently casted to float. In math [real numbers] > [integers] > [natural numbers}, why 1 is an integer? and especially why 1 is an 32 bit integer?

T foo(T)(T k) {
        return k + 3;
}
General answer will be like, above code should fricking work, without my explicit cast! And you are right, That is perfectly fine with me! :) I again ask, for this code to work, Is it necessary "3" be a 32bit integer, or a native type at all?

--

You are right, inv is a dumb function, we need a real world example.
Say we have a function with 2 overloads, which might be the reasons of precision, performance...

float foo(float m) {}
double foo(double m) {}

Now we need to call this within a generic code.

void test(T)(T m) {
        return m * foo(0.3);
}

Since 0.3 is a double, double version will be called, so we should use foo(cast(T)0.3), right? When the function foo is a popular one, what am i going to do? anything other than casting at each occurrence?
with functions like foo, code becomes like...

void test2(T)(T m) {
        T n = foo1(cast(T)0.3) / foo2(cast(T)9);
        ....
        ...
}

And everyone thinks that he is writing generic code...

Thanks!

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Reply via email to