Open CeylonMigrationBot opened 10 years ago
[@lucaswerkmeister] You could also construct that list at runtime like this (pseudo-ish code):
List<Node> getCases() {
List<Node> ret = new ArrayList<>();
ret.addAll(getTypes());
ret.addAll(getBaseMemberExpressions());
ret.sort(byAscending(Node.getLocation));
return ret;
}
[@lucaswerkmeister] Workaround (Ceylon):
CaseTypes that = ...;
MutableList<Node> casesList = ArrayList<Node>();
casesList.addAll(CeylonIterable(that.types));
casesList.addAll(CeylonIterable(that.baseMemberExpressions));
assert (nonempty cases = casesList.sort(byIncreasing(compose(Token.tokenIndex, Node.token))));
Note that it would still be better to properly record this information instead of extracting it from the tokens like this – this workaround breaks if the AST wasn’t parsed from a token stream.
[@lucaswerkmeister] Also note that in the workaround, casesList
should really be a MutableList<StaticType|BaseMemberExpression>
, but #1572 currently prevents this.
[@lucaswerkmeister] The following caseTypes
are parsed into the following AST:
As you can see, all the
BaseMemberExpression
s – the lowercase, or object, cases – now come after the “real” (uppercase) types, because the parser adds them to a separate list. This doesn’t matter for the compiler itself, because the order of case types is irrelevant, but it’s very critical for tools that operate on the source-code level (for example, a formatter).The simplest way to fix this – without restructuring the class hierarchy – would be to add a
List<Node>
toCaseTypes
that containsBaseMemberExpression
s andStaticType
s. (Of course, in Ceylon, you could make it aList<StaticType|BaseMemberExpression>
and remove the original lists. Aren’t union types awesome?)[Migrated from ceylon/ceylon-spec#947]