digitalmars.D - pred_switch manual example
- Andy Balba (16/16) Jul 29 2020 The following example taken from
- Steven Schveighoffer (4/25) Jul 29 2020 Are you importing std.algorithm.comparison?
- Seb (8/24) Jul 29 2020 Your release of GDC seems to use a very old version of Phobos.
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"(
Jul 29 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
Jul 29 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
Jul 29 2020