|
| 1 | +--- |
| 2 | +title: Used a typed hole |
| 3 | +--- |
| 4 | + |
| 5 | +## Error Message |
| 6 | + |
| 7 | +``` |
| 8 | +Example4.hs:4:7: error: [GHC-88464] |
| 9 | + • Found hole: _ :: a -> Bool |
| 10 | + Where: ‘a’ is a rigid type variable bound by |
| 11 | + the type signature for: |
| 12 | + foo :: forall a. Eq a => a -> Bool |
| 13 | + at Example4.hs:3:1-24 |
| 14 | + • In an equation for ‘foo’: foo = _ |
| 15 | + • Relevant bindings include |
| 16 | + foo :: a -> Bool (bound at Example4.hs:4:1) |
| 17 | + Constraints include Eq a (from Example4.hs:3:1-24) |
| 18 | + Valid hole fits include foo :: a -> Bool (bound at Example4.hs:4:1) |
| 19 | + | |
| 20 | +4 | foo = _ |
| 21 | + | ^ |
| 22 | +``` |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +Typed holes are very useful! They work in _expressions_ (i.e. not in patterns or types). They generate an error, ensuring that you don't forget to put some code there, but the error message is designed to give you information about what kind of code fits here. |
| 27 | + |
| 28 | +Here is the [GHC user's guide on typed holes](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/typed_holes.html). |
| 29 | + |
| 30 | +In the error message, the most important parts are: |
| 31 | + |
| 32 | +- The type of the hole: `a -> Bool` in this case. |
| 33 | +- Where the type variables come from that appear in this type; `a` here comes from `foo`'s type signature. |
| 34 | +- Relevant constraints: in this case, we know that `Eq a` holds. This list is in general not exhaustive; sometimes there are many possibly (indirectly) relevant constraints, and GHC has to make some selection of what to show. |
| 35 | +- Valid hole fits: these are suggestions from GHC of names that would typecheck in the hole. This is also in general not an exhaustive list, and typically you'll want to write code that is not quite as simple as a single variable. In this case, GHC sees that `foo` fits — which is not terribly helpful. |
| 36 | + |
| 37 | +Typed holes can be used for what is sometimes called _type-driven programming_: ask GHC what type it wants in a particular place, decide that the code should then look roughly like so (with the parts you don't yet know filled in with holes `_` again), ask GHC for the types of those holes, etc. This is mostly useful when you have very precise types in your code, typically involving some type-level programming. |
| 38 | + |
| 39 | +If you want to test your code while you still have typed holes in your code, pass the [`-fdefer-typed-holes`](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/defer_type_errors.html#ghc-flag--fdefer-typed-holes) flag to GHC. More generally, there is also the much more aggressive [`-fdefer-type-errors`](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/defer_type_errors.html#ghc-flag--fdefer-type-errors). |
0 commit comments