Should you be able to initialize a float with a char?
ab
not_a_real_address at nowhere.ab
Thu May 19 07:46:31 UTC 2022
On Thursday, 19 May 2022 at 03:46:42 UTC, Walter Bright wrote:
> On 5/18/2022 5:55 PM, max haughton wrote:
>> People do indeed (I'd question whether it's routine in a good
>> D program, I'd flag it in code review) manipulate characters
>> as integers, but I think there's something to be said for
>> forcing people to go char -> suitable integer -> char.
>
> Casts are a common source of bugs, not correctness. This is
> because it is forced override of the type system. If the types
> change due to refactoring, the cast may no longer be correct,
> but the programmer will have no way of knowing.
This can be solved by a cast with explicit source and destination
type, e.g.
auto cast_from(From,To,Real)(Real a)
{
static if (is (From==Real))
return cast(To) a;
else
pragma(msg, "Wrong types");
}
void main()
{
import std.range, std.stdio;
short a=1;
int b=cast_from!(short,int)(a);
bool c=1;
// int d=cast_from!(short,int)(a); // compile time error
writeln("Test: ", b);
}
More information about the Digitalmars-d
mailing list