Suggestion: Operator `in` for slices

Nick Treleaven nick at geany.org
Tue Jan 4 18:23:33 UTC 2022


On Monday, 3 January 2022 at 00:15:53 UTC, Era Scarecrow wrote:
>  Then maybe we in should be implemented; Have it check if you 
> have it sorted (*unless you do assumesorted template*). If it 
> is sorted naturally use BinarySearch, and if not it would 
> probably do a linear search **BUT** give a warning message and 
> file/line number so it can be fixed/traced? (*Or just make it 
> have to be Binary Search and asserts out if it isn't sorted*)
>
>  As for bool vs pointer return... probably return a pointer as 
> that can easily be tested as bool for no extra cost.

It would make sense to implement `in` for SortedRange, but there 
is no use case for returning a pointer. You already have the 
value on the left hand side, the only reason for a pointer would 
be to mutate the element in the range. Mutating that would 
potentially stop the range being in order, violating the point of 
SortedRange.


More information about the Digitalmars-d mailing list