Ich versuche, einen Typ zu erstellen, der garantiert, dass eine Zeichenfolge weniger als N Zeichen lang ist.Konnte Art '*' nicht mit 'Nat' übereinstimmen
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
import GHC.TypeLits (Symbol, Nat, KnownNat, natVal, KnownSymbol, symbolVal)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Proxy (Proxy(..))
data TextMax (n :: Nat) = TextMax Text
deriving (Show)
textMax :: KnownNat n => Text -> Maybe (TextMax n)
textMax t
| Text.length t <= (fromIntegral $ natVal (Proxy :: Proxy n)) = Just (TextMax t)
| otherwise = Nothing
Dies gibt den Fehler:
src/Simple/Reporting/Metro2/TextMax.hs:18:50: error:
• Couldn't match kind ‘*’ with ‘Nat’
When matching the kind of ‘Proxy’
• In the first argument of ‘natVal’, namely ‘(Proxy :: Proxy n)’
In the second argument of ‘($)’, namely ‘natVal (Proxy :: Proxy n)’
In the second argument of ‘(<=)’, namely
‘(fromIntegral $ natVal (Proxy :: Proxy n))’
ich diesen Fehler nicht verstehen. Warum funktioniert es nicht? I've used almost this exact same code to get the natVal of n in other places and it works.
Gibt es einen besseren Weg, dies zu tun?
Nebenbemerkung: auf dem durch Drehen ' PolyKinds 'Erweiterung, ghc ist milder mit der Idee eines Poly-kinded 'Proxy' und die Fehlermeldung wird anders und macht es viel klarer, dass es ein typisches' ScopedTypeVariables'-Problem ist. – gallais