3
我正在使用GHC 8.2.1。我有以下模塊:爲什麼這個HasField實例沒有被解析?
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Moat (Moat,moat) where
import GHC.Records (HasField(..))
newtype Moat r = Moat r
moat :: r -> Moat r
moat = Moat
instance HasField s r v => HasField s (Moat r) v where
getField (Moat r) = getField @s r
這等一個:
module Foo (Foo(..)) where
data Foo a = Foo { getDims :: (Int, Int), getData :: [a] }
我的問題是,當我有兩個模塊進口,我嘗試做這樣的事情:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
import Moat
import Foo
import GHC.Records
oops :: (Int,Int)
oops = getField @"getDims" (moat (Foo (5,5) ['c']))
我得到這個錯誤:
No instance for (HasField "getDims" (Moat (Foo Char)) (Int, Int))
arising from a use of ‘getField’
爲什麼HasField
實例無法解析?