Somehow give a warning when trying to assign a size_t/ptrdiff_t to int or uint

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 17 04:26:02 PDT 2016


On 4/17/2016 2:29 AM, WebFreak001 wrote:
> It's annoying to fix all these `int index = str.indexOf("something")` to
> size_t/ptrdiff_t because you started writing the code thinking that indexOf
> returns an integer even though it returns a ptrdiff_t. When porting code from
> 32bit to 64bit you need to fix all these lines which can quickly become quite
> complex from functions where you don't expect them to return size_t or ptrdiff_t
>
> Adding a warning when trying to do something like `int i =
> funcReturningPtrdiff();` would make porting easier because you would already
> spot the issues on the OS/architecture you are working on. This also affects
> porting windows to linux as DMD on windows uses 32bit by default and on linux it
> uses the architecture you downloaded as default.

Making weird special cases usually winds up having unintended bad consequences 
in the future, leading to ever more special cases and complexity.

Some solutions you can use:

1. size_t index = str.indexOf("something")

2. auto index = str.indexOf("something")

3. compile for 64 bits as the default

4. regularly compile for 32 and 64 bits

My experience is that over time one gets used to writing 32/64 bit portable code 
naturally, and the minor irritations don't happen anymore. D isn't a language 
that tries to hide the differences, and one should be aware of them.


More information about the Digitalmars-d mailing list