Last active
March 27, 2020 21:07
-
-
Save boj/7540bb0edc458ed6c8742f375e0558b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE TypeSynonymInstances #-} | |
{-# LANGUAGE TypeFamilies #-} | |
module Main where | |
import Data.Coerce | |
import Data.Tagged | |
data Validated | |
data Unvalidated | |
data MyError = MyError String deriving (Show) | |
newtype Name a = Name { unName :: String } deriving (Show) | |
-- data NameTag | |
-- type Name a = Tagged NameTag String | |
-- mkName :: String -> Name Unvalidated | |
-- mkName = Tagged | |
class Validatable a where | |
type ValidationError a :: * | |
validate :: a Unvalidated -> Either (ValidationError a) (a Validated) | |
instance Validatable Name where | |
type ValidationError Name = MyError | |
validate n | |
| length (unName n) >= 3 = Right (coerce n) | |
| otherwise = Left (MyError "too short") | |
main :: IO () | |
main = do | |
print $ validate (Name "abcd") | |
print $ validate (Name "a") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment