length's type.

Kevin Bailey keraba at yahoo.com
Fri Feb 9 15:39:10 UTC 2024


On Friday, 9 February 2024 at 11:00:09 UTC, thinkunix wrote:
>
> 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.

Hi thinkunix,

I did interpret your post as critical. Sorry if it wasn't 
intended to be and my reply had a little too much heat. I still 
think my reply was at least accurate, so replies below.

> 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?

The program that I was writing was most elegant doing that. 
Obviously I wasn't doing something as simple as the example. The 
post is simply to highlight the issue.

Unfortunately I can't find the examples now. The code has been 
altered so grepping isn't finding it and, since AoC has 25 days, 
I'm not sure which ones it was. It /might/ have been this, where 
'where_to_start' is signed and can be negative. It's a weird 
index of indexes thing, and quite unconventional.

         // Try it in the remaining groups.
         for (auto i = where_to_start; i < num_ss.length; ++i)

> * why use auto which made the type different than what .length 
> is?

Google "almost always auto" for why you should prefer it - don't 
miss Herb Sutter's post - but as someone else pointed out, it's 
no better with 'int' *or* 'ulong'.

The "best" solution is to cast the returned length to long. This 
makes it work and, unless you're counting the atoms in the 
universe, should be sufficient on a reasonable machine.

This is why I brought up the example. zjh was lamenting having to 
cast, as am I, much less think this hard about it.

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

I wasn't asking a question. I know how to code this in D and I 
made it work. My post was to highlight the completely unnecessary 
need to cast.

What happens when there's a more reasonable example? What happens 
when you need to compare 2 library function results when one is 
signed and the other not? Would you even know that you had to 
cast one? Or would you just get strange results and not know why? 
The issue exists completely outside of my example.

Since you sound new, I'll mention that, yes, what I'm proposing 
can be a hair slower. But so what? 99 of a 100 programs won't 
notice and, if it does, your profiler will tell you where, you 
add the cast (or you add it ahead time, since that's what we have 
to do now), done.

I understand that it is almost certainly too late for D but the 
world seems ready for an alternative to C++ and lots of languages 
are coming. This is just part of that discussion, right?



More information about the Digitalmars-d-learn mailing list