2017-10-19 31 views
4

與LTS-9.2(及單身-2.1)使用堆棧,我有這樣的:如何推廣一個函數,它返回一個字符串與單身?

$(singletons [d| 
     data EventScans 
     = PassThrough 
     | SuiteProgress 

     -- cn :: EventScans -> [Char] 
     cn PassThrough = "all-events" 
     cn SuiteProgress = "suite-progress" 
    |]) 

我不能給這個函數類型簽名並推斷出的類型簽名是cn :: IsString t => EventScans -> t。 AFAIK IsString不單獨化。

如果沒有cn上的類型簽名,我會按預期得到Cn類型的系列。如果我嘗試添加我獲得的類型簽名:

* Expected kind `[Char]', but `"all-events"' has kind `Symbol' 
* In the type `"all-events"' 
    In the type family declaration for `Cn' (haskell-stack-ghc) 

什麼是正確的方法來做到這一點?

編輯如果我嘗試包括類型簽名cn :: IsString t => EventScans -> t我得到以下錯誤:

Variable `a_agPb' used as both a kind and a type 
Did you intend to use TypeInType? (haskell-stack-ghc) 
Not in scope: type constructor or class `SIsString' 
    Perhaps you meant `IsString' (imported from Data.String) (haskell-stack-ghc) 
+0

你的方式沒問題 - 'IsString'被singletons處理得很好(至少對我來說 - 這段代碼工作正常)。請注意,推斷的類型是'..-> String',除非你啓用了'OverloadedStrings',所以如果你真的想要簡單的返回類型,就把這個代碼放在沒有它的模塊中。或者,只需要'cn':: EventScans - > String; cn'= cn'某處。 – user2407038

+0

@ user2407038爲了使問題更清晰,我編輯了一下。 –

+0

錯誤似乎告訴你設置-XTypeInType。這有幫助嗎? – HTNW

回答

相關問題