Jump to page: 1 2
Thread overview
Preventing implicit conversion
Nov 04, 2015
ixid
Nov 04, 2015
ixid
Nov 04, 2015
Daniel Kozak
Nov 04, 2015
ixid
Nov 04, 2015
Maxim Fomin
Nov 04, 2015
ixid
Nov 05, 2015
Maxim Fomin
Nov 05, 2015
Jonathan M Davis
Nov 05, 2015
ixid
Nov 05, 2015
Adam D. Ruppe
Nov 06, 2015
Jonathan M Davis
November 04, 2015
Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?
November 04, 2015
On Wednesday, 4 November 2015 at 14:27:49 UTC, ixid wrote:
> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?

Also does this not seem inconsistent:

    ushort a = ushort.max, b = ushort.max;


    a += b; // Compiles fine
    a = a + b; // Error: cannot implicitly convert expression (cast(int)a + cast(int)b) of type int to ushort
November 04, 2015
V Wed, 04 Nov 2015 14:27:45 +0000
ixid via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
napsáno:

> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?

http://dlang.org/phobos/std_typecons.html#.Typedef


November 04, 2015
On Wednesday, 4 November 2015 at 17:26:04 UTC, Daniel Kozak wrote:
> V Wed, 04 Nov 2015 14:27:45 +0000
> ixid via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> napsáno:
>
>> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?
>
> http://dlang.org/phobos/std_typecons.html#.Typedef

That doesn't appear to prevent implicit conversion. Making two bools (or ubytes etc) that are Typedef and adding them together still results in an int.
November 04, 2015
On Wednesday, 4 November 2015 at 14:27:49 UTC, ixid wrote:
> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?

Only with library solution. Implicit conversions are built into language.
November 04, 2015
On Wednesday, 4 November 2015 at 19:09:42 UTC, Maxim Fomin wrote:
> On Wednesday, 4 November 2015 at 14:27:49 UTC, ixid wrote:
>> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?
>
> Only with library solution. Implicit conversions are built into language.

Doesn't that seem rather limiting and unnecessary?
November 05, 2015
On Wednesday, 4 November 2015 at 21:22:04 UTC, ixid wrote:
> On Wednesday, 4 November 2015 at 19:09:42 UTC, Maxim Fomin wrote:
>> On Wednesday, 4 November 2015 at 14:27:49 UTC, ixid wrote:
>>> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?
>>
>> Only with library solution. Implicit conversions are built into language.
>
> Doesn't that seem rather limiting and unnecessary?

Well, indeed it often produces confusion (this is inherited from C for compatibility purpose).
November 05, 2015
On Wednesday, November 04, 2015 21:22:02 ixid via Digitalmars-d-learn wrote:
> On Wednesday, 4 November 2015 at 19:09:42 UTC, Maxim Fomin wrote:
> > On Wednesday, 4 November 2015 at 14:27:49 UTC, ixid wrote:
> >> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?
> >
> > Only with library solution. Implicit conversions are built into language.
>
> Doesn't that seem rather limiting and unnecessary?

Why? You can't affect what conversions do and don't work for the built-in types in _any_ language that I've ever used, and I've never heard of a language that allowed anything like that. If you want different conversion rules, you need to create a user-defined type that defines the conversions you want. That's pretty normal.

And AFAIK, there aren't very many folks trying to avoid the built-in implicit conversions in D, particularly since D eliminated the various implicit narrowing conversions that you get in C/C++.

- Jonathan M Davis

November 05, 2015
On Thursday, 5 November 2015 at 05:41:46 UTC, Jonathan M Davis wrote:
> On Wednesday, November 04, 2015 21:22:02 ixid via Digitalmars-d-learn wrote:
>> On Wednesday, 4 November 2015 at 19:09:42 UTC, Maxim Fomin wrote:
>> > On Wednesday, 4 November 2015 at 14:27:49 UTC, ixid wrote:
>> >> Is there an elegant way of avoiding implicit conversion to int when you're using shorter types?
>> >
>> > Only with library solution. Implicit conversions are built into language.
>>
>> Doesn't that seem rather limiting and unnecessary?
>
> Why? You can't affect what conversions do and don't work for the built-in types in _any_ language that I've ever used, and I've never heard of a language that allowed anything like that. If you want different conversion rules, you need to create a user-defined type that defines the conversions you want. That's pretty normal.
>
> And AFAIK, there aren't very many folks trying to avoid the built-in implicit conversions in D, particularly since D eliminated the various implicit narrowing conversions that you get in C/C++.
>
> - Jonathan M Davis

In C++ I can add two shorts together without having to use a cast to assign the result to one of the two shorts. It just seems super clunky not to be able to do basic operations on basic types without casts everywhere.
November 05, 2015
On Thursday, 5 November 2015 at 09:33:40 UTC, ixid wrote:

> In C++ I can add two shorts together without having to use a cast to assign the result to one of the two shorts. It just seems super clunky not to be able to do basic operations on basic types without casts everywhere.

+1
If automatic shrink is droped from the C legacy stuff, so interger propagation should also be dropped (or changed to propagate no further than to the actual size of a type). D has a far better type system, throw away bad old C habits!

-> this would also make the defect comparison of signed to unsigned types visible for small types and hopefully force the introduction of the correct comparison!
« First   ‹ Prev
1 2