typed holes
コードを書いている際,未実装の部分を仮置きしておける機能.
code:haskell
import Control.Monad (when)
main :: IO ()
main = do
let cond = True
when cond $ do
_doSomething -- TODO
code:plaintext
typed_holes.hs:8:5: error:
• Found hole: _doSomething :: IO ()
Or perhaps ‘_doSomething’ is mis-spelled, or not in scope
• In a stmt of a 'do' block: _doSomething
In the second argument of ‘($)’, namely ‘do _doSomething’
In a stmt of a 'do' block: when cond $ do _doSomething
• Relevant bindings include
cond :: Bool (bound at typed_holes.hs:5:7)
main :: IO () (bound at typed_holes.hs:4:1)
Valid hole fits include
main :: IO () (bound at typed_holes.hs:4:1)
readLn :: forall a. Read a => IO a
with readLn @()
(imported from ‘Prelude’ at typed_holes.hs:1:1
(and originally defined in ‘System.IO’))
mempty :: forall a. Monoid a => a
with mempty @(IO ())
(imported from ‘Prelude’ at typed_holes.hs:1:1
(and originally defined in ‘GHC.Base’))
|
8 | _doSomething
| ^^^^^^^^^^^^
hole _doSomething には IO () という型が付いているのがわかる,また,なんとその hole に当てはまりそうな値まで提案してくれる (valid hole fits; GHC 8.6+)
typed hole が見つかった場合,コンパイラはその時点がエラーを報告して終了するが,-fdefer-typed-holes を指定することにより,コンパイルを通し,エラーを実行時まで遅延させることができる.すなわち,typed hole が実行時に評価された場合にランタイムエラーが発生する.開発時にとりあえず実装を進めたい場合に便利.