+class Polymorphic(Type):
+
+ def __init__(self, default_type, switch_expr, switch_types):
+ Type.__init__(self, default_type.expr)
+ self.default_type = default_type
+ self.switch_expr = switch_expr
+ self.switch_types = switch_types
+
+ def visit(self, visitor, *args, **kwargs):
+ return visitor.visit_polymorphic(self, *args, **kwargs)
+
+ def iterswitch(self):
+ cases = [['default']]
+ types = [self.default_type]
+
+ for expr, type in self.switch_types:
+ case = 'case %s' % expr
+ try:
+ i = types.index(type)
+ except ValueError:
+ cases.append([case])
+ types.append(type)
+ else:
+ cases[i].append(case)
+
+ return zip(cases, types)
+
+