Meta:

I know immutable is a good thing, but don't you think `immutable _` is a bit unnecessary in this case?


Some answer, choose the one you prefer:

1) Yes, it's totally useless because the _ variable is not even used inside the loop body! So sorry, I'm always so pedantic.

2) It's necessary, don't you see that? You don't need to mutate that variable, so it's better for it be immutable. A simple rule to follow is to make const/immutable all variables that don't need to mutate, to make code simpler and safer. There's no real reason to break that general rule in this case.

3) Just like the integer '5' a range of values as 0 .. 1000 is an immutable value. So a variable that scans such range should be immutable. If you really want to mutate such variable you should add a modifier like "mutable" or "mut" or something. Another common trap in D coding is iterating on an array of structs with foreach, mutating the current struct and forgetting that you are mutating only a _copy_ of the items. Unfortunately there is no mutable keyword in D, and Walter rejected all this idea. So the next best thing it to always put "immutable" at the foreach variable, unless you want to mutate it or if you can't use const/immutable for some other reason.

Probably I can invent you more creative answers if you want.

Bear hugs,
bearophile

Reply via email to