Hi,
Can I match on constructors in a list and get a list of that specific constructor? Something like this:
list[MyType] lst = [a(1), b(1), c(1), a(2), b(2), c(2)];
if ([A*, b(_) B*, C*] := lst) {
println("got the b's: <B>");
}
Thanks,
Jasper
The short answer is no. The reason is that b(_) is not a legal type. There are, fortunately, other ways to solve this.
Assuming that MyType is for instance:
data MyType = a(int i) | b(int i) | c(int i);
and that your goal is to filter out all b(_) occurrences from the list, something like
[ X | X:b(_) <- lst]; (with value [ b(1), b(2)])
may solve your problem. Observe the X:b(_) that matches each element of MyType with b(_) and assigns it to X when it matches. See http://tutor.rascal-mpl.org/Courses/Rascal/Patterns/Abstract/Labelled/Labelled.html for details.
Asked: Jan 24
Seen: 15 times
Last updated: Jan 24
Copyright CWI, 2010-2012. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.