Thread overview | |||||
---|---|---|---|---|---|
|
July 29, 2020 pred_switch manual example | ||||
---|---|---|---|---|
| ||||
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"( |
July 29, 2020 Re: pred_switch manual example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Balba | 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
|
July 29, 2020 Re: pred_switch manual example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Balba | 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 |
Copyright © 1999-2021 by the D Language Foundation