pred_switch manual example

Steven Schveighoffer schveiguy at gmail.com
Wed Jul 29 21:54:17 UTC 2020


On 7/29/20 5:46 PM, Andy Balba wrote:
> 
> The following example taken from 
> https://dlang.org/library/std/algorithm/comparison/pred_switch.html
> 
> doesn't compile under gdc...Is there a way to fix it ?
> 
> 
> int factorial(int n)
> {
>      return n.predSwitch!"a <= b"(
>          -1, {throw new Exception("Can not calculate n! for n < 0");}(),
>          0, 1, // 0! = 1
>          n * factorial(n - 1) // n! = n * (n - 1)! for n >= 0
>          );
> }
> writeln(factorial(3));
> 
> The gdc compiler error is
>   error: no property 'predSwitch' for type 'int'
>       return n.predSwitch!"a <= b"(
> 

Are you importing std.algorithm.comparison?

This kind of question probably belongs on the learn forum.

-Steve


More information about the Digitalmars-d mailing list