pred_switch manual example

Seb seb at wilzba.ch
Wed Jul 29 22:17:16 UTC 2020


On Wednesday, 29 July 2020 at 21:46:04 UTC, 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"(

Your release of GDC seems to use a very old version of Phobos.
You need to have Phobos >= 2.067 (released in 2015) for this to 
work, see e.g. https://run.dlang.io/is/cAFJoB
On Ubuntu/Debian you should be able to at least get a GDC 
libphobos of 2.076.

Alternatively, as you might expect it works in dmd or ldc: 
https://run.dlang.io/is/CpFfkQ


More information about the Digitalmars-d mailing list