rectifiedIndexOf()

Salih Dincer salihdb at hotmail.com
Sat May 6 18:58:54 UTC 2023


On Saturday, 6 May 2023 at 16:39:13 UTC, Quirin Schroll wrote:
> On Friday, 5 May 2023 at 00:19:01 UTC, Salih Dincer wrote:
>> It has a traditional very beautiful function: indexOf() but it 
>> has an important problem: returns -1 when it can't find 'c'...
>
> That is bad design. It should return an optional type or 
> something like that, but D doesn’t have those. C++ has a lot of 
> similar functions and I guess that was one reason why C++17 
> introduced an *init-statment* to `if`. D could do the same.
>
> ```d
> if (auto index = arr.indexOf('c'); index >= 0) …
> ```

In fact, there is worse!  The intent is to prevent leaking 
outside the scope of if-else.  But it doesn't work in the else 
block and we get the error undefined identifier `num`.

```d
   if(auto num = imported!"std.random".uniform!int.max % 2)
   {
     assert(num > 0);
   } else {
     //assert(num == 0);
   }
```

I would like these issues to be resolved, but we spend our energy 
on more difficult things!

SDB at 79


More information about the Digitalmars-d mailing list