Can someone explain to me the design choices for this function definition?

e-y-e via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 22 13:35:13 PDT 2016


The function in question is std.algorithm.searching's until [1].

Here are the definitions:


Until!(pred, Range, Sentinel)
until(alias pred = "a == b", Range, Sentinel)
(Range range, Sentinel sentinel, OpenRight openRight = 
OpenRight.yes)
if (!is(Sentinel == OpenRight));


and:


Until!(pred, Range, void)
until(alias pred, Range)
(Range range, OpenRight openRight = OpenRight.yes);


Now the first thing that came to mind was that it seemed odd to 
have openRight be a runtime flag instead of a compile-time flag. 
But that didn't hinder my usage of it, so I went ahead and called 
the function like so:

range.until!(e => e > f)(No.openRight)

and I got an error saying the function could not be deduced, so I 
went looking in the source code and found:


enum OpenRight
{
     no,
     yes
}


But I had assumed OpenRight was an alias for a Flag type [2], so 
I had called it wrong.

Does anyone have any idea why the function is defined this way?

1. Why is openRight a runtime flag? Is there really a use case 
for this?

2. Why is openRight not a Flag type?

here is the definition I was expecting:


alias OpenRight = Flag!"openRight";

auto until(alias pred = "a == b", OpenRight openRight = 
Yes.openRight, Range, Sentinel)
(Range range, Sentinel sentinel);

auto until(alias pred, OpenRight openRight = Yes.openRight)(Range 
range);

[1] https://dlang.org/phobos/std_algorithm_searching.html#until
[2] https://dlang.org/phobos/std_typecons.html#.Flag


More information about the Digitalmars-d-learn mailing list