Concatenation of ubyte[] to char[] works, but assignation doesn't

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 5 22:38:16 PDT 2015


On Monday, October 05, 2015 11:48:51 Marc Schütz via Digitalmars-d-learn wrote:
> On Monday, 5 October 2015 at 10:30:02 UTC, Jonathan M Davis wrote:
> > On Monday, October 05, 2015 09:07:34 Marc Schütz via
> > Digitalmars-d-learn wrote:
> >> I don't think math would be a problem. There are some obvious
> >> rules that would likely just work with most existing code:
> >>
> >> char + int = char
> >> char - int = char
> >> char - char = int
> >> char + char = ERROR
> >
> > That depends on whether VRP can figure out that the result will
> > fit. Otherwise, you'd be stuck with int. As it stands, all of
> > these would just end up with int, I believe, though if they're
> > assigned to a char and the int is a constant, then VRP may kick
> > in and make a cast unnecessary.
> >
>
> I think Walter's argument for allowing the int <-> char
> conversions was that they are necessary to allow arithmetic. My
> rules show that it works without these implicit conversions.
>
> VRP is a different problem, though. AFAICS, in the following
> code, VRP either is smart enough, or it isn't, no matter whether
> char implicitly converts to int.
>
> int diff = 'a' - 'A';
> char c = 'A';
> char d = c + diff;

Your suggestion only works by assuming that the result will fit in a char,
which doesn't fit at all with how coversions are currently done in D. It
would allow for narrowing conversions which lost data. And there's no way
that Walter would go for that (and I don't think that he should). VRP solves
the problem insofar as it can guarantee that the result will fit in the
target type and thus reduces the need for casting, but simply assuming that
char + int will fit in a char just doesn't work unless we're going to allow
narrowing conversions to lose data, which we aren't.

If we were to allow the specific conversions that you're suggesting but only
when VRP was used, then that could work, though it does make the implicit
rules even screwier, becauses it becomes very dependent on how the int that
you're trying to assign to a char was generated in the first place (straight
assignment wouldn't work, but '0' - 40 would, whereas 'a' + 500 wouldn't,
etc.). VRP already makes it a bit funky as it is, though mostly in a
straightforward manner.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list