Inference from pattern matching
See original GitHub issueWould it possible (and would it make sense) to add inference for pattern matching?
For example:
function update(state, action) {
switch (action.type) {
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
default:
return state;
}
}
let nextState = update(0, { type: "INCREMENT" });
Here, it’s clear from reading the code that action.type
has a defined set of expected values.
Hegel does infer from usage that action
must have a type
property.
But it does not infer that the type
is a string - although it’s clear to a person familiar with pattern matching that, not only is a string property expected, but there is also an expected set of constant string types.
I’m sure this is non-trivial, but I’m wondering if it’s even possible and whether it would make sense? 🙂
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Pattern‐matching and inference in story understanding
In SAM, a method of pattern-matching is used, first to decide to which situation a story refers, then to follow the story through...
Read more >Pattern Matching - The Scala Programming Language
Pattern matching tests whether a given value (or sequence of values) has the shape defined by a pattern, and, if it does, binds...
Read more >Type inference for unique pattern matching - ACM Digital Library
Regular expression patterns provide a natural, declarative way to express constraints on semistructured data and to extract relevant information from it.
Read more >Type inference for Haskell, part 14 - Jeremy Mikkola
There's another way to match integers called an “n + k” pattern. This matches any positive integer that is greater than or equal...
Read more >Type Inference for Regular Expression Pattern Matching
Abstract: An important feature of statically typed XML programming languages is the type inference of variables in regular expression patterns, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mindplay-dk that sounds like a good idea
and would imply that this code
will produce this interface
instead of this one
which would be very good for inferring types of js code instead of using poorly implemented
.d.ts
files from DefinitelyTypedBecause JavaScript? Type-checking is compile-time, so somebody will always be able to pass anything.
But there’s no such thing as an exhaustive switch/case in JS - so no matter what I do, handling
default
or not, you can always pass anything to this function. Even if fordefault
there was athrow
statement, it’s still a branch, there’s still a control flow, and therefore a type, in this case just an exception.Reducer patterns like these are super common in JS now - is there some way we can handle them?