| |
| Posted by Jonathan M Davis in reply to Dom DiSc | PermalinkReply |
|
Jonathan M Davis
Posted in reply to Dom DiSc
| On Monday, September 23, 2024 5:08:37 PM MDT Dom DiSc via Digitalmars-d wrote:
> On Monday, 23 September 2024 at 05:34:51 UTC, Walter Bright wrote:
> > I agree that it seems to make sense making 4 an `int`.
>
> A number literal should always be of the smallest type that can
> represent it - it will be implicitly converted to larger types
> anyway.
> So, 4 should be ubyte (as only negative literals should be of
> signed type and there is no smaller type in D than ubyte - else
> it should be of type "nibble" (4bit)).
4 will work in any context that requires a ubyte, because the compiler will know that it will fit. The issue is what it defaults to. In this case, it's size_t, because it has to do with memory size, and you really want stuff like T.sizeof * numberOfTs to result in size_t, because it represents the amount of memory being used, and if it's not size_t, it risks being truncated. It would be particularly bad if it were treated as ubyte given how little memory that would be.
So, the issue really isn't the value itself. That will convert to other sizes just fine, particularly since it's known at compile time. Rather, what matters is what you get when you do math on it, and that really needs to be size_t, because it's used for memory sizes.
- Jonathan M Davis
|