This is very much me thinking out loud, but I have found myself bouncing between 3 tools to gain confidence while implementing complex method signatures:
- tests
- types
- pattern matching
Pattern matching is core the implementation, as it allows to cleanly branch the various signature types/groups. While it does throw errors for unmatched patterns (presuming no else
branch), it doesn't "publish" the implicit shape of the method for the outside world. This is a power of types. Not only do they provide errors for unmatched inputs, they publish an explicit shape for the method to the outside world. Tests, finally, exercise the code paths to ensure that inputs that match the various shape configurations produce the desired outcome.
Currently, there is no code-level connection between any of these 3 layers. I would love to somehow draw them more closely together.
When thinking about connecting types and patterns, I am wondering if it would be possible to somehow define a named type that could act as a pattern, e.g.
class SelectionParser
SelectableColumn = Symbol
SelectableExpression = String
SelectableAssociation = Hash
Selectable = _Union(SelectableColumn, SelectableExpression, SelectableAssociation)
def self.call(input)
case input
in SelectableColumn => column
parse_column(column)
in SelectableExpression => expression
parse_expression(expression)
in SelectableAssociation => association
parse_association(association)
end
end
end
I don't currently have a thought for connecting types and tests, but even just types as patterns would be amazing.