length's type.

thinkunix thinkunix at zoho.com
Fri Feb 9 11:00:09 UTC 2024


Kevin Bailey via Digitalmars-d-learn wrote:
> On Thursday, 8 February 2024 at 08:23:12 UTC, thinkunix wrote:
>>
>> I would never write code like this.
> 
> By all means, please share with us how you would have written that just 
> as elegantly but "correct".

First off I, I am just a beginner with D.  I joined this list to try to
learn more about the language not to but heads with experts.  I'm sorry
if you took my response that way.

My post was merely to show how, with my rudimentary knowledge, I could
get the loop to execute 4 times, which appeared (to me) to be the intent 
of your code.  Thank you for the exercise.  I learned more about the D 
type system.

I said I would not write code like that because:
* why start at -1 if array indexes start at 0?
* why use auto which made the type different than what .length is?

You provided no context, or comment indicated what you were trying
to achieve by starting with -1.  Clearly I didn't understand your
intent.


>> It would also break if the array 'something' had more than int.max 
>> elements.
> 
> Then don't cast it to an int. First of all, why didn't you cast it to a 
> long? 

I only "cast(int)something.length" so the type would match the type
that "auto i = -1" would get, which was int, and this was to prevent
comparing incompatible types, which caused the conversion, and the
loop not to execute at all.

As a beginner, I would expect that if you mismatch types, you can
expect bad things to happen, and this is probably true in any language.
If your issue is that the compiler didn't catch this, shouldn't you
raise the issue on a compiler internals list?  Maybe I've misunderstood
the purpose of d-learn "Questions about learning and using D".

scot



More information about the Digitalmars-d-learn mailing list