Error: castSwitch
none via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 6 20:55:03 PDT 2016
import std.algorithm.iteration : map;
import std.algorithm : castSwitch;
import std.format : format;
class A { int value; this(int value) { this.value = value; }}
interface I { }
class B : I { }
Object[] arr = [new A(5), new B(), null];
auto results = arr.map!(castSwitch!(
(A a) => "class A with a value of %d".format(a.value),
(I i) => "derived from I",
(B b) => "class B",
() => "null reference",
))();
The codes get nonsense error like:
/home/bthach/dlang/dmd-2.071.0/linux/bin64/../../src/phobos/std/algorithm/comparison.d(277): Error: no property 'format' for type 'string'
But when I changed the order:
[...]
(B b) => "class B",
(I i) => "derived from I",
Suprisingly, it works.
More information about the Digitalmars-d-learn
mailing list