literals

so so at so.do
Sun Mar 28 11:29:52 PDT 2010


> What you want is implicit function template instantiation based on the  
> return type.  D doesn't do that, it doesn't even allow overloading based  
> on the return type.  However, you can infer the return type using auto:
>
> auto inv(T)(T m)
> {
>     return 1.0/m;
> }
>
> -Steve

Hello!

I guess, i am unable to express myself.

In code :

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

If we forget the rules of default literals that C derived languages have,  
just for a second.

And enforce our own little rule.
- no implicit casts

With this in mind this code says; i gave you T, give me T in return,  
perfectly clear i guess?
Now when you call the function with a floating point, real, double,  
float..., you will get what you asked for.
Now what about other types? Say you call it with a non floating type, when  
compiler tries to divide the first thing
it will encounter that one of two elements of the operation is a  
non-float, since we enforced a rule, it gives warning or error.

Thanks!

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



More information about the Digitalmars-d mailing list