scripts: csv.py: Improved default typechecking in RExpr

Now, by default, an error is raised if any branch of an expr has an
inconsistent type.

This isn't always what we want. The ternary operator, for example,
doesn't really care if the condition's type doesn't match the branch
arms. But it's a good default, and special cases can always override the
type function with their own explicit typechecking.
This commit is contained in:
Christopher Haster
2024-11-12 12:02:55 -06:00
parent f31f3fdd68
commit effc959ea9

View File

@@ -346,7 +346,10 @@ class RExpr:
return set(it.chain.from_iterable(v.fields() for v in self))
def type(self, types={}):
return self.a.type(types)
t = self.a.type(types)
if not all(t == v.type(types) for v in it.islice(self, 1, None)):
raise RExpr.Error("mismatched types? %r" % self)
return t
def fold(self, types={}):
return self.a.fold(types)