length's type.

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jan 18 07:44:00 UTC 2024


On Wednesday, January 17, 2024 11:33:48 PM MST zjh via Digitalmars-d-learn 
wrote:
> On Thursday, 18 January 2024 at 04:30:33 UTC, Jonathan M Davis
> wrote:
> but for a lot of code, using auto and size_t makes it
>
> > so that you don't need to use int, and it would be a big
> > problem in general if the language made length int.
>
> It's hard to imagine that an `'int'` needs to be replaced with
> `'auto'`.

It's very common in D code to do stuff like

auto a = foo();

or

auto len = arr.length;

That way, you automatically get the correct type. Obviously, there are cases
where you need to force a particular type, and that can require casting, but
inferring types often simplifies code. It's _very_ common in idiomatic D
code to use auto when you don't need to force a specific type. And when
dealing with arrays, it's very typical to use either auto or size_t, because
then you get the correct integer type regardless of the platform, and then
you only need to worry about casting to int in cases where you actually need
int for whatever reason.

But regardless of whether you want to use auto, there are very good reasons
for why length is size_t, and C/C++ made exactly the same choice for the
same reasons. You can certainly disagree with that choice, but it's not the
kind of thing that stands much chance of ever being changed.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list