length's type.

Siarhei Siamashka siarhei.siamashka at gmail.com
Sun Jan 28 06:34:13 UTC 2024


On Thursday, 18 January 2024 at 02:55:37 UTC, zjh wrote:
> Can you change the type of 'length' from 'ulong' to 'int', so I 
> haven't to convert it every time!

The explicit conversion `.length.to!int` has an extra benefit of 
doing a runtime check to ensure that the length value actually 
fits in an `int` variable. Just in case if somebody tries to use 
your code to process a 3GB array of bytes.

It's more correct to rewrite the code to expect `size_t`. But one 
needs to be very careful about implementing it correctly, because 
silent casts between signed and unsigned data types may ruin your 
day. It's a major source of bugs, similar to the one discussed in 
https://forum.dlang.org/thread/vyvbrtmyelududcvukfb@forum.dlang.org


More information about the Digitalmars-d-learn mailing list