Bu, GHC'lerde 8.0'dan önce mümkün değil, ancak the (as of this writing) just-released GHC 8.0.1 ekle custom type errors için destek.
fikri sadece fonksiyonu error :: String -> a
gibi, şimdi, in GHC.TypeLits
, bir ile herhangi tip yaşar tipi ailesini
type family TypeError (msg :: ErrorMessage) :: k
sahip terimi düzeyinde bir hata ile her türlü yaşar, yani hata yazın. ErrorMessage
tipi çok basit:
data ErrorMessage = Text Symbol
| ShowType t
| ErrorMessage :<>: ErrorMessage
| ErrorMessage :$$: ErrorMessage
(:<>:)
yapıcı iki hata iletileri yatay birleştirir; (:$$:)
yapıcısı bunları dikey olarak birleştirir. Diğer iki kurucu, söylediklerini yaparlar.
Böylece, sizin örnekte, bir TypeError
ile son durumda doldurabilirsiniz; Örneğin,
type family Testf a where
Testf Char = IO()
Testf String = IO()
Testf a = TypeError ( Text "‘Testf’ didn't match"
:$$: Text "when applied to the type ‘"
:<>: ShowType a :<>: Text "’")
Ardından, doğru
testfInt :: Testf Int
testfInt = pure()
tanımlarken kırdığı
....hs:19:12: error: …
• ‘Testf’ didn't match
when applied to the type ‘Int’
• In the expression: pure()
In an equation for ‘testfInt’: testfInt = pure()
Compilation failed.
Not hatasıyla başarısız olur tip Testf Int
de pure()
kullanmaya çalışıyor tanımlayan
testfInt :: Testf Int
testfInt = undefined
(ya da testfInt = testfInt
ile benzer şekilde) iyi çalıştı.
{-# LANGUAGE UndecidableInstances, TypeFamilies, DataKinds, TypeOperators #-}
import GHC.TypeLits
type family Testf a where
Testf Char = IO()
Testf String = IO()
Testf a = TypeError ( Text "‘Testf’ didn't match"
:$$: Text "when applied to the type ‘"
:<>: ShowType a :<>: Text "’")
testfChar :: Testf Char
testfChar = putStrLn "Testf Char"
testfString :: Testf Char
testfString = putStrLn "Testf String"
-- Error here!
testfInt :: Testf Int
testfInt = putStrLn "Int"
GHC 8.0 hataları iyi görünmesi için özel tip denetleyicisi desteği ile TypeLits' 'böyle bir şey sunuyor:
İşte tam bir örnek kaynak dosya. – dfeuer
İyi bul, buna referans ekledi. –