generic + numeric + literals = abomination

so so at so.do
Sun Mar 28 07:28:09 PDT 2010


Why a generic code have to be ugly?
At this age of compilers and languages, and the capabilities of DMD?
Why that many casts? or implicit casts?
DMD already doing it behind the scenes with constant folding,
not sure but i think literals stay that way mostly because of C  
compatibility!

Thanks.

On Sun, 28 Mar 2010 19:09:18 +0400, #ponce <nospam at nospam.com> wrote:

> so Wrote:
>
>> Basically what i am asking is hmmm, ability to write generic constants?  
>> :)
>>
>> Thanks!
>
> Hi,
>
> When writing generic FP code i always use real literals and cast to T,  
> or int. I suggest doing this.
> Like:
>
> T exp3(T)(T x)
> {
> 	if (x < cast(T)(-1.15365L))
> 	{
> 		return 0;
> 	}
> 	else
> 	{
> 		return cast(T)1.L + x * (cast(T)1.0L + x * (cast(T)0.5L + x *  
> cast(T)0.3333333333333L));
> 	}
> }
>
>
> I assume the compiler will be able to optimize the cast in release mode.
>
> If you really wan't to prevent this cast in debug mode, you can simply  
> go with:
>
> const T myConst = 0.3L;
>
> then the cast will be compile-time.
>


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


More information about the Digitalmars-d-learn mailing list