Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type safety in the reducer #190

Open
bakkot opened this issue Jul 1, 2018 · 0 comments
Open

Type safety in the reducer #190

bakkot opened this issue Jul 1, 2018 · 0 comments

Comments

@bakkot
Copy link
Member

bakkot commented Jul 1, 2018

Currently the reducer has methods which look like

public State reduceClassDeclaration(
  ClassDeclaration node,
  State name,
  Maybe<State> _super,
  ImmutableList<State> elements
)

where of course ClassDeclaration is shaped like

class ClassDeclaration(
  BindingIdentifier name,
  Maybe<Expression> _super,
  ImmutableList<ClassElement> elements
)

There is a guarantee provided by the director, but not the type system, that the fields of the node and the noninitial arguments of the method line up, in the sense that node._super.isJust() == _super.isJust(), node.elements.length == elements.length, etc.

The fact that this isn't enforced by the type system leads to a lot of unchecked .fromJust()s. We could avoid this by defining, for each node type, a node-with-associated-data type along the lines of

<T> class ClassDeclarationWithT(
  Pair<BindingIdentifierWith, T> name,
  Maybe<Pair<Expression, T>> _super,
  ImmutableList<Pair<ClassElementWith, T>> elements
)

and then making the signature of methods in Reducer<State> be

public State reduceClassDeclaration(
  ClassDeclarationWithT<State> state
)

This does have the downside that you don't get a reference to the original node, which the LazyCloneReducer among other interfaces wast - for example, you need the original node to look up its location in the data structure provided by ParserWithLocation. I suppose it could always be passed as a second argument for those cases.

You can of course automatically convert between reducers of this type and reducers of the existing type, though going from reducers of the existing type to reducers of this type requires having all those unchecked .fromJust()s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant