March 28, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Everything comes to this... Why "3" is an int? Why "0.3 is a double? I guess these constraints was there before generic coding comes out, and we are just stuck with it! If these sound so naive, sorry about it, I don't know compiler/language history. Thanks! On Sun, 28 Mar 2010 12:29:17 +0400, bearophile <bearophileHUGS@lycos.com> wrote: > so: > >> With one exception yes, i want all 3 test pass with your fix to implicit >> cast. >> You know, we are trying to write generic code. > > I don't understand your problem/needs, sorry. > > Bye, > bearophile -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 28, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | so:
> Well, i am having hard time explaining, it is not a surprise that you don't understand.
I think I have understood you this time. Writing skills are important for a programmer :-) Is this what you are asking for? But it's not very good code:
import std.stdio: writeln;
struct Vector(T) {
this(T m) { mm = m; }
this(int m) { mm = cast(T)m; }
Vector opBinary(string op:"*", T2)(T2 m) {
return Vector(mm * m);
}
T mm;
}
void test(T)() {
auto v = Vector!T(5);
auto u = v * 3;
writeln("u: ", u.mm);
}
void main() {
test!ubyte();
test!ushort();
test!uint();
test!ulong();
}
Bye,
bearophile
|
March 28, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | You didn't change anything there, just back to original code, now just enabled implicit cast again! Please read my next replies, everything should be clear now. :) Thanks! On Sun, 28 Mar 2010 14:32:21 +0400, bearophile <bearophileHUGS@lycos.com> wrote: > so: > >> Well, i am having hard time explaining, it is not a surprise that you >> don't understand. > > I think I have understood you this time. Writing skills are important for a programmer :-) > Is this what you are asking for? But it's not very good code: > > import std.stdio: writeln; > > struct Vector(T) { > this(T m) { mm = m; } > this(int m) { mm = cast(T)m; } > Vector opBinary(string op:"*", T2)(T2 m) { > return Vector(mm * m); > } > T mm; > } > > void test(T)() { > auto v = Vector!T(5); > auto u = v * 3; > writeln("u: ", u.mm); > } > > void main() { > test!ubyte(); > test!ushort(); > test!uint(); > test!ulong(); > } > > Bye, > bearophile -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 28, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | Le 28/03/10 10:57, so a écrit :
> Well, i am having hard time explaining, it is not a surprise that you
> don't understand.
> To make things clearer, lets forget floats for a seconds and change your
> code to standard unsigned types.
>
>
> import std.stdio: writeln;
>
> struct Vector(T) {
> this(T m) { mm = m; }
> Vector opBinary(string op:"*", T2)(T2 m) if(is(T2 == T)) {
> return Vector(mm * m);
> }
> T mm;
> }
Have you checked std.traits, where isIntegral!T, isFloatingPoint!T, etc. are defined? Could these, used as template constraints, solve your problem?
|
March 28, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to biozic | I can't think of any solution they might provide sorry. You know there are C++ equivalents, for years and they didn't solve any problems above. Maybe i can use them to disable implicit casting, but then again program won't compile. :P Thanks! On Sun, 28 Mar 2010 18:09:21 +0400, biozic <dransic@free.fr> wrote: > Le 28/03/10 10:57, so a écrit : >> Well, i am having hard time explaining, it is not a surprise that you >> don't understand. >> To make things clearer, lets forget floats for a seconds and change your >> code to standard unsigned types. >> >> >> import std.stdio: writeln; >> >> struct Vector(T) { >> this(T m) { mm = m; } >> Vector opBinary(string op:"*", T2)(T2 m) if(is(T2 == T)) { >> return Vector(mm * m); >> } >> T mm; >> } > > Have you checked std.traits, where isIntegral!T, isFloatingPoint!T, etc. are defined? Could these, used as template constraints, solve your problem? > -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ |
March 28, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to so | 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.
|
March 28, 2010 Re: generic + numeric + literals = abomination | ||||
---|---|---|---|---|
| ||||
Posted in reply to #ponce | 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@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/ |
Copyright © 1999-2021 by the D Language Foundation