Why MapResult.empty() is not const in Phobos?

Exil Exil at gmall.com
Sat Jul 6 18:36:50 UTC 2019


On Saturday, 6 July 2019 at 18:01:12 UTC, Jonathan M Davis wrote:
> On Saturday, July 6, 2019 10:18:49 AM MDT Exil via 
> Digitalmars-d wrote:
>> On Saturday, 6 July 2019 at 06:56:22 UTC, Jonathan M Davis 
>> wrote:
>> > On Friday, July 5, 2019 11:37:10 PM MDT valmat via
>> >
>> > Digitalmars-d wrote:
>> >> Minimal examples when it broke code: 
>> >> https://run.dlang.io/is/HbEfkJ 
>> >> https://run.dlang.io/is/4h1B6Z
>> >
>> > In general, it can't be empty, because that would require 
>> > that the range it's wrapping have a const empty, and most 
>> > don't. The only way that map (or any wrapper range) can have 
>> > a const empty would be if it goes to the effort of 
>> > duplicating the function and uses static ifs to determine 
>> > whether the version that's const will work or whether the 
>> > version that's not const needs to be compiled in. And since 
>> > const ranges are basically unusable, because they can't be 
>> > iterated, going to that level of effort and making the code 
>> > messy in that manner is arguably a waste of time.
>> >
>> > - Jonathan M Davis
>>
>> So phobos doesn't properly use const either, officially. Just 
>> one more reason not to use const on the pile.
>
> There really isn't a way to use const "properly" with ranges.

As in annotating functions you want to guarantee won't make 
modifications to the object. Sure const ranges might be useless 
the way they are designed. But I still want to guarantee my 
empty() function doesn't make any changes. I can't do that 
because phobos doesn't properly annotate functions with const.


More information about the Digitalmars-d mailing list